Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

82.9 KB
Raw Blame
/**
 * feishu/outbound.ts
 * ---------------------------------------------------------------------------
 * Outbound primitives backed by Lark.Client. Exposes the surface that
 * `FeishuIM` re-exports + a few internal helpers (bindClient / addReaction /
 * removeReaction / patchCardRaw) consumed by sibling modules.
 *
 * Targets: p2p sends use `receive_id_type: 'open_id'`. Group lane userIds
 * (`g/{chatId}[/{threadId}]`, see codec.ts) resolve to a reply anchor
 * (im.message.reply — 话题内自动落回话题) or `receive_id_type: 'chat_id'`.
 *
 * Note (parity gap from legacy replyClient.ts): the inline `xdt-image://` /
 * `xdt-file://` markdown rewriting (upload local images → img element, split
 * out file links into separate file messages) is NOT included here. That
 * behaviour is business-policy and belongs in the host orchestrator. When
 * orchestrator wants to embed images, it should pre-resolve `xdt-image://`
 * URLs to feishu image_keys via `uploadImage` + build the card JSON with
 * `img` elements directly.
 */

import { AsyncLocalStorage } from 'node:async_hooks';
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import { open as openFile, type FileHandle } from 'node:fs/promises';
import path from 'node:path';
import * as Lark from '@larksuiteoapi/node-sdk';

import { getLog, scopeSlot } from './moduleScope.js';
import { buildInteractiveCardV1, buildMarkdownCardV2 } from './cards.js';
import { decodeLaneUserId } from './codec.js';
import * as ownerGuard from './ownerGuard.js';
import { parseIncoming } from './incomingContent.js';
import type { AttachmentRef } from './incomingContent.js';
import { messages as transportMessages } from './messages.js';
import type {
  IMMessageEvent,
  InteractiveCardSpec,
  SendFileResult,
} from '../types.js';
import type { BotCredentials } from './internal-types.js';

/** 30 MB per file — feishu's upper limit for `im.file.create`. */
const FEISHU_FILE_SIZE_LIMIT = 30 * 1024 * 1024;
/** 10 MB per image when sending as `msg_type:image`. */
const FEISHU_IMAGE_MAX_BYTES = 10 * 1024 * 1024;
// PowerShell's first Add-Type invocation can be slow on hosted Windows
// runners (Defender scans the generated compiler artifacts). Keep the helper
// bounded and fail closed, but allow that one-time startup to finish.
const HANDLE_PATH_HELPER_TIMEOUT_MS = 15_000;
const HANDLE_PATH_HELPER_MAX_BYTES = 256 * 1024;

/**
 * Windows does not expose a `/proc/self/fd` equivalent to Node. The helper gets
 * the already-open file as stdin, then asks the kernel for that inherited
 * handle's final path. It never accepts an Agent-controlled path argument.
 */
const WINDOWS_HANDLE_PATH_SCRIPT = String.raw`
$utf8 = [System.Text.UTF8Encoding]::new($false)
[Console]::OutputEncoding = $utf8
Add-Type -TypeDefinition @'
using System;
using System.Text;
using System.Runtime.InteropServices;
public static class CindyHandlePath {
  [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
  public static extern uint GetFinalPathNameByHandle(IntPtr handle, StringBuilder path, uint size, uint flags);
  [DllImport("kernel32.dll")]
  public static extern IntPtr GetStdHandle(int kind);
}
'@
$handle = [CindyHandlePath]::GetStdHandle(-10)
$path = New-Object System.Text.StringBuilder 32768
$length = [CindyHandlePath]::GetFinalPathNameByHandle($handle, $path, $path.Capacity, 0)
if ($length -eq 0 -or $length -ge $path.Capacity) { exit 1 }
[Console]::Out.Write($path.ToString())
`;
const WINDOWS_HANDLE_PATH_COMMAND = Buffer.from(
  WINDOWS_HANDLE_PATH_SCRIPT,
  'utf16le',
).toString('base64');

/**
 * Prove that an already-open source handle is reachable from an already-open
 * directory handle without resolving any Agent-controlled absolute path. Each
 * component is opened relative to the previous handle with reparse traversal
 * disabled; the final native file identity must equal the source handle.
 */
const WINDOWS_HANDLE_CONTAINMENT_SCRIPT = String.raw`
$utf8 = [System.Text.UTF8Encoding]::new($false)
[Console]::OutputEncoding = $utf8
Add-Type -TypeDefinition @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public static class CindyHandleContainment {
  private const uint FILE_READ_ATTRIBUTES = 0x00000080;
  private const uint SYNCHRONIZE = 0x00100000;
  private const uint FILE_SHARE_ALL = 0x00000007;
  private const uint FILE_SHARE_READ_WRITE = 0x00000003;
  private const uint FILE_OPEN = 0x00000001;
  private const uint FILE_DIRECTORY_FILE = 0x00000001;
  private const uint FILE_NON_DIRECTORY_FILE = 0x00000040;
  private const uint FILE_SYNCHRONOUS_IO_NONALERT = 0x00000020;
  private const uint FILE_OPEN_REPARSE_POINT = 0x00200000;
  private const uint FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;

  [StructLayout(LayoutKind.Sequential)]
  private struct UNICODE_STRING {
    public ushort Length;
    public ushort MaximumLength;
    public IntPtr Buffer;
  }

  [StructLayout(LayoutKind.Sequential)]
  private struct OBJECT_ATTRIBUTES {
    public int Length;
    public IntPtr RootDirectory;
    public IntPtr ObjectName;
    public uint Attributes;
    public IntPtr SecurityDescriptor;
    public IntPtr SecurityQualityOfService;
  }

  [StructLayout(LayoutKind.Sequential)]
  private struct IO_STATUS_BLOCK {
    public IntPtr Status;
    public IntPtr Information;
  }

  [StructLayout(LayoutKind.Sequential)]
  private struct FILE_ATTRIBUTE_TAG_INFO {
    public uint FileAttributes;
    public uint ReparseTag;
  }

  [StructLayout(LayoutKind.Sequential)]
  private struct BY_HANDLE_FILE_INFORMATION {
    public uint FileAttributes;
    public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
    public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
    public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
    public uint VolumeSerialNumber;
    public uint FileSizeHigh;
    public uint FileSizeLow;
    public uint NumberOfLinks;
    public uint FileIndexHigh;
    public uint FileIndexLow;
  }

  [DllImport("kernel32.dll")]
  private static extern IntPtr GetStdHandle(int kind);

  [DllImport("kernel32.dll", SetLastError=true)]
  private static extern bool GetFileInformationByHandle(
    IntPtr handle,
    out BY_HANDLE_FILE_INFORMATION info
  );

  [DllImport("kernel32.dll", SetLastError=true)]
  private static extern bool GetFileInformationByHandleEx(
    IntPtr handle,
    int infoClass,
    out FILE_ATTRIBUTE_TAG_INFO info,
    uint size
  );

  [DllImport("ntdll.dll")]
  private static extern int NtCreateFile(
    out SafeFileHandle fileHandle,
    uint desiredAccess,
    ref OBJECT_ATTRIBUTES objectAttributes,
    out IO_STATUS_BLOCK ioStatusBlock,
    IntPtr allocationSize,
    uint fileAttributes,
    uint shareAccess,
    uint createDisposition,
    uint createOptions,
    IntPtr eaBuffer,
    uint eaLength
  );

  private static SafeFileHandle OpenRelative(IntPtr root, string name, bool directory) {
    IntPtr nameBuffer = IntPtr.Zero;
    IntPtr unicodePointer = IntPtr.Zero;
    try {
      nameBuffer = Marshal.StringToHGlobalUni(name);
      var unicode = new UNICODE_STRING {
        Length = checked((ushort)(name.Length * 2)),
        MaximumLength = checked((ushort)((name.Length + 1) * 2)),
        Buffer = nameBuffer
      };
      unicodePointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UNICODE_STRING)));
      Marshal.StructureToPtr(unicode, unicodePointer, false);
      var attributes = new OBJECT_ATTRIBUTES {
        Length = Marshal.SizeOf(typeof(OBJECT_ATTRIBUTES)),
        RootDirectory = root,
        ObjectName = unicodePointer,
        Attributes = 0,
        SecurityDescriptor = IntPtr.Zero,
        SecurityQualityOfService = IntPtr.Zero
      };
      IO_STATUS_BLOCK statusBlock;
      SafeFileHandle opened;
      uint options = FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT |
        (directory ? FILE_DIRECTORY_FILE : FILE_NON_DIRECTORY_FILE);
      int status = NtCreateFile(
        out opened,
        FILE_READ_ATTRIBUTES | SYNCHRONIZE,
        ref attributes,
        out statusBlock,
        IntPtr.Zero,
        0,
        directory ? FILE_SHARE_READ_WRITE : FILE_SHARE_ALL,
        FILE_OPEN,
        options,
        IntPtr.Zero,
        0
      );
      if (status < 0 || opened == null || opened.IsInvalid) {
        if (opened != null) opened.Dispose();
        return null;
      }
      FILE_ATTRIBUTE_TAG_INFO tag;
      if (!GetFileInformationByHandleEx(
          opened.DangerousGetHandle(),
          9,
          out tag,
          (uint)Marshal.SizeOf(typeof(FILE_ATTRIBUTE_TAG_INFO))) ||
          (tag.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
        opened.Dispose();
        return null;
      }
      return opened;
    } catch {
      return null;
    } finally {
      if (unicodePointer != IntPtr.Zero) Marshal.FreeHGlobal(unicodePointer);
      if (nameBuffer != IntPtr.Zero) Marshal.FreeHGlobal(nameBuffer);
    }
  }

  private static bool SameFile(IntPtr left, IntPtr right) {
    BY_HANDLE_FILE_INFORMATION a;
    BY_HANDLE_FILE_INFORMATION b;
    if (!GetFileInformationByHandle(left, out a) || !GetFileInformationByHandle(right, out b)) {
      return false;
    }
    if (a.FileIndexHigh == 0 && a.FileIndexLow == 0) return false;
    if (b.FileIndexHigh == 0 && b.FileIndexLow == 0) return false;
    return a.VolumeSerialNumber == b.VolumeSerialNumber &&
      a.FileIndexHigh == b.FileIndexHigh &&
      a.FileIndexLow == b.FileIndexLow;
  }

  public static bool Check(string[] segments) {
    if (segments == null || segments.Length == 0) return false;
    IntPtr root = GetStdHandle(-10);
    IntPtr source = GetStdHandle(-12);
    if (root == IntPtr.Zero || root == new IntPtr(-1) ||
        source == IntPtr.Zero || source == new IntPtr(-1)) return false;

    var opened = new List<SafeFileHandle>();
    try {
      IntPtr parent = root;
      for (int i = 0; i < segments.Length; i++) {
        string segment = segments[i];
        if (String.IsNullOrEmpty(segment) || segment == "." || segment == ".." ||
            segment.IndexOf('\0') >= 0 || segment.IndexOf('\\') >= 0 ||
            segment.IndexOf('/') >= 0 || segment.IndexOf(':') >= 0) return false;
        SafeFileHandle next = OpenRelative(parent, segment, i < segments.Length - 1);
        if (next == null) return false;
        opened.Add(next);
        parent = next.DangerousGetHandle();
      }
      return opened.Count > 0 && SameFile(opened[opened.Count - 1].DangerousGetHandle(), source);
    } finally {
      for (int i = opened.Count - 1; i >= 0; i--) opened[i].Dispose();
    }
  }
}
'@
try {
  $json = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($env:CINDY_HANDLE_SEGMENTS))
  $segments = @((ConvertFrom-Json -InputObject $json))
  if ([CindyHandleContainment]::Check([string[]]$segments)) {
    [Console]::Out.Write('1')
    exit 0
  }
} catch {}
exit 1
`;
const WINDOWS_HANDLE_CONTAINMENT_COMMAND = Buffer.from(
  WINDOWS_HANDLE_CONTAINMENT_SCRIPT,
  'utf16le',
).toString('base64');

/**
 * Darwin `/dev/fd` is fdescfs: `chdir("/dev/fd/N")` is ENOTDIR even when N is
 * a directory fd. Walk with `openat` from the inherited root handle instead.
 */
const DARWIN_HANDLE_CONTAINMENT_SCRIPT = String.raw`
use strict;
use warnings;
use Fcntl qw(O_RDONLY O_NONBLOCK O_DIRECTORY O_NOFOLLOW :mode);

use constant SYS_openat => 463;

sub fail_closed { exit 1; }

my @segments = @ARGV;
fail_closed() unless @segments;
for my $segment (@segments) {
  fail_closed() if !defined($segment) || $segment eq '' || $segment eq '.' ||
    $segment eq '..' || index($segment, '/') >= 0 || index($segment, "\0") >= 0;
}

my @source = stat(STDERR);
fail_closed() unless @source && S_ISREG($source[2]) && $source[1] != 0;
my $parent = fileno(STDIN);
fail_closed() unless defined $parent && $parent >= 0;

my @opened;
for (my $index = 0; $index < @segments; $index += 1) {
  my $directory = $index < @segments - 1;
  my $flags = O_RDONLY | O_NOFOLLOW | O_NONBLOCK;
  $flags |= O_DIRECTORY if $directory;
  my $fd = syscall(SYS_openat, $parent, $segments[$index], $flags, 0);
  fail_closed() if !defined($fd) || $fd < 0;
  open(my $handle, '<&=', $fd) or fail_closed();
  push @opened, $handle;
  my @opened_stat = stat($handle);
  fail_closed() unless @opened_stat;
  if ($directory) {
    fail_closed() unless S_ISDIR($opened_stat[2]);
    $parent = $fd;
  } else {
    fail_closed() unless S_ISREG($opened_stat[2]) && $opened_stat[1] != 0 &&
      $opened_stat[0] == $source[0] && $opened_stat[1] == $source[1];
  }
}

print STDOUT '1';
`;

const out_SLOT = Symbol('feishu.out');
function createOutState() {
  return {
    client: null as Lark.Client | null,
    creds: null as BotCredentials | null,
    lastBoundCreds: null as BotCredentials | null,
    accountEpoch: 0,
    laneAnchors: new Map<string, LaneAnchorState>(),
    patchableOpeners: new Map<string, string>(),
    openerTriggers: new Map<string, string>(),
    deferredOpenerConsumes: [] as DeferredOpenerConsume[],
    evictedOpeners: [] as string[],
    openThreadByTrigger: new Map< string, { ts: number; promise: Promise<OpenThreadOutcome> } >(),
    cardLanes: new Map<string, { ts: number; lane: string }>(),
  };
}
/** 本实例(FeishuScope)的模块状态; 没有 scope 时落在默认实例上。 */
const S = () => scopeSlot(out_SLOT, createOutState);

/** 最近一次 bind 的账号身份 — 跨 unbind 保留, 用于判「账号是否真替换」。 */

function sameBoundAccount(next: BotCredentials): boolean {
  const bound = S().lastBoundCreds;
  return bound !== null && bound.appId === next.appId && bound.service === next.service;
}

/**
 * 账号代次 — 账号身份变化(换账号 bind / 明确清凭证)时递增: 在途的排空/兜底
 * 续段据此丢弃, 旧账号的终态不得经新账号的 client 呈现。
 */

function clearAccountScopedOutboundState(): void {
  S().laneAnchors.clear();
  S().patchableOpeners.clear();
  S().openerTriggers.clear();
  clearDeferredOpenerConsumes();
  S().accountEpoch += 1;
}

/**
 * 明确清除凭证/登出路径(clearAndDisconnect)用: 与 transport 重连区分 —
 * 清掉账号态并重置 lastBoundCreds, 之后重新保存相同 appId/service 不会被
 * 误判为同账号重连(登出前的 opener/锚点不得被新一轮会话认领)。
 */
export function forgetBoundAccount(): void {
  clearAccountScopedOutboundState();
  S().lastBoundCreds = null;
}

/** 当前账号代次(清凭证递增) — 在途排空续段比对用。 */
export function getAccountEpoch(): number {
  return S().accountEpoch;
}

export function bindClient(c: BotCredentials): void {
  // 账号真正替换(appId/service 变化)才清开场白卡与话题锚点: 同账号 transport
  // 重连(reconnectSavedCredentials: stop → start 同凭证)时 turn 仍在跑, 未消费
  // 的 opener / 锚点必须仍可被认领, 否则答案无处 patch、话题出站没有 reply 根。
  if (!sameBoundAccount(c)) clearAccountScopedOutboundState();
  S().lastBoundCreds = c;
  S().creds = c;
  S().client = new Lark.Client({
    appId: c.appId,
    appSecret: c.appSecret,
    domain: c.service === 'lark' ? Lark.Domain.Lark : Lark.Domain.Feishu,
  });
}

export function unbindClient(): void {
  S().client = null;
  S().creds = null;
  S().cardLanes.clear();
  // patchableOpeners / laneAnchors 不随 unbind 清空(见 bindClient 的账号替换清理)。
}

// ── group lane reply anchors ──────────────────────────────────────────────────
// 群 lane 的「答案挂回提问」: wsClient 在 owner 触发时 push 触发消息 id,
// 出站按回合领取(流式建卡时 advance)。话题 lane 的**所有**出站都走 reply
// 锚点 — 被回复消息在话题里, 回复自动落回话题(会话与挂回一次解决);
// 普通群 lane 只有回合首条出站 reply(引用式"挂在提问下"), 后续 chat_id 直发。
// 账号替换时清空 — 同账号 transport 重连保留, 避免进行中的 turn 丢掉话题锚点。

interface LaneAnchorState {
  /** 待领取的触发消息 id FIFO(多条消息先后触发同一 lane 各自排队)。 */
  queue: string[];
  /** 当前回合持有的锚点。 */
  held: string | null;
  /** Trigger owned by the claimed opener, separate from a later pending opener. */
  heldOpenerTrigger?: string;
  /** 普通群: held 是否已用于引用回复(用过则后续 chat_id 直发)。 */
  quotedHeld: boolean;
  /**
   * held 是群主流触发消息(开场白撤回后的回拨锚点)时置位 — 对它 reply 必须带
   * reply_in_thread 才会落回话题(触发消息本人在群主流)。
   */
  inThreadReply: boolean;
}


/** wsClient 在 owner 群触发时登记回挂锚点。 */
export function pushReplyAnchor(laneUserId: string, messageId: string): void {
  const state = S().laneAnchors.get(laneUserId) ?? {
    queue: [],
    held: null,
    quotedHeld: false,
    inThreadReply: false,
  };
  state.queue.push(messageId);
  S().laneAnchors.set(laneUserId, state);
}

// ── patchable opener (开话题开场白卡 = 本轮流式卡) ──────────────────────────
// 群主流 @ 开话题时, 开场白是一张「思考中」占位卡; 本轮流式卡不再新建一条
// 回复, 而是直接 patch 这张卡 — 话题里第一条可见内容就是答案。lane → opener
// 卡的映射由 wsClient 在 openThread 成功后登记, streamingText.start 认领。


/**
 * 开场白卡对应的触发消息 id(lane → 群主流触发消息)。开场白 patch/替换失败
 * 被撤回后, 话题里不再有合法锚点 — 回拨到触发消息并带 reply_in_thread 回复
 * 才能落回话题(触发消息本人在群主流)。
 */

export function pushPatchableOpener(
  laneUserId: string,
  messageId: string,
  triggerMessageId?: string,
): void {
  S().patchableOpeners.set(laneUserId, messageId);
  if (triggerMessageId) S().openerTriggers.set(laneUserId, triggerMessageId);
  else S().openerTriggers.delete(laneUserId);
}

/**
 * 认领本 lane 的开场白卡: 存在则把它推进为已持有锚点(后续 reply 挂它, 仍落
 * 在话题内)并返回消息 id, streamingText.start 拿它直接 patch; 不存在(开话题
 * 失败的降级群 lane / 话题内 @)返回 null, 走新建流式卡的老路。
 */
export function claimPatchableOpener(laneUserId: string): string | null {
  const openerId = S().patchableOpeners.get(laneUserId);
  if (!openerId) return null;
  S().patchableOpeners.delete(laneUserId);
  const state = S().laneAnchors.get(laneUserId) ?? {
    queue: [],
    held: null,
    quotedHeld: false,
    inThreadReply: false,
  };
  // 锚点队列头正常就是这张开场白卡; 对不上说明顺序被破坏, 保守起见仍保留队列。
  if (state.queue[0] === openerId) state.queue.shift();
  state.held = openerId;
  state.heldOpenerTrigger = S().openerTriggers.get(laneUserId);
  S().openerTriggers.delete(laneUserId);
  state.quotedHeld = true;
  state.inThreadReply = false;
  S().laneAnchors.set(laneUserId, state);
  return openerId;
}

/**
 * 重连空窗(outbound client 已解绑)时到达的开场白卡消费暂存 — 同账号重连后
 * 由 FeishuIM 排空(见 drainDeferredOpenerConsumes): 若此时直接认领, 后续
 * patch/撤回全部失败且注册被永久移除; 若完全放弃, 本轮终态丢失且注册残留
 * 会被下一条消息误认领。上限防御, 超限丢最旧。
 */
export type DeferredOpenerConsume = (
  | { userId: string; openerId: string; markdown: string }
  | { userId: string; openerId: string; spec: InteractiveCardSpec }
) & { epoch: number };

const MAX_DEFERRED_OPENER_CONSUMES = 50;

/**
 * 容量淘汰的开场白卡 id — 条目被挤掉时其 opener 已被原子预留(不可再认领),
 * 静默删除会留下永久「思考中」卡: 记录 id, 连接恢复排空时撤回。
 */
// 上限防御撤除: 每个条目只是短消息 id, 且连接恢复排空即清 — 有界淘汰会
// 再次静默丢卡(阈值从 51 推迟到 101 而已), 不留可收口记录违背设计。

/** 账号替换时清空暂存消费(属旧账号的开场白卡, 新账号不需要)。 */
function clearDeferredOpenerConsumes(): void {
  S().deferredOpenerConsumes.length = 0;
  S().evictedOpeners.length = 0;
}

export function deferOpenerConsume(
  entry: { userId: string; openerId: string } & (
    | { markdown: string }
    | { spec: InteractiveCardSpec }
  ),
): void {
  S().deferredOpenerConsumes.push({ ...entry, epoch: S().accountEpoch });
  while (S().deferredOpenerConsumes.length > MAX_DEFERRED_OPENER_CONSUMES) {
    const evicted = S().deferredOpenerConsumes.shift();
    if (evicted) {
      S().evictedOpeners.push(evicted.openerId);
    }
  }
}

/** 取出被容量淘汰的开场白卡 id(清空), 连接恢复排空时撤回。 */
export function drainEvictedOpeners(): string[] {
  return S().evictedOpeners.splice(0, S().evictedOpeners.length);
}

/** 取出全部暂存的消费(清空), 调用方在连接就绪后排空。 */
export function drainDeferredOpenerConsumes(): DeferredOpenerConsume[] {
  return S().deferredOpenerConsumes.splice(0, S().deferredOpenerConsumes.length);
}

/**
 * 取出并移除某 lane 的匹配暂存消费 — 调用方的兜底发送在连接恢复后执行时,
 * 优先就地收口被预留的 opener(见 FeishuIM 的发送包装): patch/替换成功即
 * 不再另发、不留卡; 无匹配返回 undefined。
 */
export function takeMatchingDeferredOpenerConsume(
  userId: string,
  kind: 'markdown' | 'spec',
): DeferredOpenerConsume | undefined {
  for (let i = S().deferredOpenerConsumes.length - 1; i >= 0; i--) {
    const entry = S().deferredOpenerConsumes[i]!;
    if (entry.userId !== userId) continue;
    if (kind === 'markdown' && !('markdown' in entry)) continue;
    if (kind === 'spec' && !('spec' in entry)) continue;
    S().deferredOpenerConsumes.splice(i, 1);
    return entry;
  }
  return undefined;
}

/** 按 openerId 取出暂存消费 — 原兜底发送认领本轮条目, 避免同 lane 后续发送误领。 */
export function takeDeferredOpenerConsumeById(openerId: string): DeferredOpenerConsume | undefined {
  const index = S().deferredOpenerConsumes.findIndex((entry) => entry.openerId === openerId);
  if (index < 0) return undefined;
  return S().deferredOpenerConsumes.splice(index, 1)[0];
}

/**
 * 返回该 lane 上 pending opener 的触发消息 id(没有则返回 undefined)。
 * 调用方用它判断「这张思考卡是不是本轮消息创建的」— 空文本终态等场景下
 * 不应消费上一轮遗留的 opener。
 */
export function getOpenerTrigger(laneUserId: string): string | undefined {
  return S().openerTriggers.get(laneUserId);
}

/**
 * patch/替换失败撤回开场白卡后调用: 把 held 锚点回拨到触发消息(带
 * reply_in_thread 标记)。兜底发送向触发消息 reply 且 reply_in_thread=true,
 * 仍落回话题 — 而不是向已删除的开场白卡 reply 失败。无触发记录返回 false。
 */
export function rearmAnchorToTrigger(laneUserId: string, expectedOpenerId: string): boolean {
  // A delayed recall must not replace the anchor claimed by a newer round.
  const state = S().laneAnchors.get(laneUserId);
  if (!state || state.held !== expectedOpenerId) return false;
  const triggerId = state.heldOpenerTrigger;
  if (!triggerId) return false;
  delete state.heldOpenerTrigger;
  state.held = triggerId;
  state.quotedHeld = true;
  state.inThreadReply = true;
  S().laneAnchors.set(laneUserId, state);
  return true;
}

type SendTarget =
  | { kind: 'open_id'; id: string }
  | { kind: 'chat_id'; id: string }
  | { kind: 'reply'; messageId: string; replyInThread?: boolean };

/**
 * userId → 本次出站目标。
 *
 * advanceRound = true(流式建卡 — 每个 agent 回合的首条出站)时强制从 FIFO
 * 领取新锚点; 其它出站沿用已持有的锚点, 没有才领取。回合边界 transport 感知
 * 不到, 以「流式建卡」为回合锚点领取点在本编排下成立: agent turn 恒以流式卡
 * 开场, 一次性回复(slash 提示等)则一条消息领取一次。
 */
function resolveSendTarget(userId: string, opts?: { advanceRound?: boolean; threadTs?: string }): SendTarget | null {
  const lane = decodeLaneUserId(userId);
  if (!lane && opts?.threadTs) {
    if (!opts.threadTs.startsWith('om_')) throw new Error('Invalid Feishu reply message id');
    return { kind: 'reply', messageId: opts.threadTs, replyInThread: true };
  }
  if (!lane) return { kind: 'open_id', id: userId };

  const state = S().laneAnchors.get(userId) ?? {
    queue: [],
    held: null,
    quotedHeld: false,
    inThreadReply: false,
  };
  S().laneAnchors.set(userId, state);
  // 领取条件: advanceRound(流式建卡 = 新回合)必领; 其余出站只在完全没有
  // 持有时领(首次一次性回复)。回合中途的卡片/文件**不领** — 队列里可能已经
  // 排着下一轮的触发锚点, 中途领取会把下一轮的锚点偷来错挂。队列空时保留
  // held: 话题 lane 复用旧锚点仍落回正确话题(话题内任意消息都是合法锚点)。
  if ((opts?.advanceRound || !state.held) && state.queue.length > 0) {
    state.held = state.queue.shift() ?? null;
    delete state.heldOpenerTrigger;
    state.quotedHeld = false;
    state.inThreadReply = false;
  }

  if (lane.threadId) {
    // 话题 lane: 必须 reply 锚点才能落回话题; 没有锚点宁可失败也不能把
    // 消息发进群主流(位置错误比丢失更糟)。held 是回拨的群主流触发消息时
    // 带 reply_in_thread 回复才能落回话题。
    return state.held
      ? { kind: 'reply', messageId: state.held, replyInThread: state.inThreadReply || undefined }
      : null;
  }
  if (state.held && !state.quotedHeld) {
    state.quotedHeld = true;
    return { kind: 'reply', messageId: state.held };
  }
  return { kind: 'chat_id', id: lane.chatId };
}

/** 统一出站: target 三形态 → message.create(open_id/chat_id) 或 message.reply。 */
async function createMessage(
  target: SendTarget,
  msgType: string,
  content: string,
  uuid?: string,
): Promise<{ messageId: string; chatId?: string }> {
  const c = ensureClient();
  if (target.kind === 'reply') {
    const res = await c.im.v1.message.reply({
      path: { message_id: target.messageId },
      data: {
        content,
        msg_type: msgType,
        // 回拨到群主流触发消息的锚点: 必须 reply_in_thread 才落回话题。
        ...(target.replyInThread ? { reply_in_thread: true } : {}),
      },
    });
    const id = res.data?.message_id ?? '';
    if (!id) throw new Error('[feishu/outbound] reply: no message_id in response');
    return { messageId: id };
  }
  const res = await c.im.v1.message.create({
    params: { receive_id_type: target.kind },
    data: { receive_id: target.id, msg_type: msgType, content, ...(uuid ? { uuid } : {}) },
  });
  const id = res.data?.message_id ?? '';
  if (!id) throw new Error('[feishu/outbound] create: no message_id in response');
  return { messageId: id, chatId: res.data?.chat_id };
}

/** Notification receipt used by the host to link replies to the originating session. */
export async function sendNotification(userId: string, markdown: string): Promise<{ messageId: string; chatId?: string }> {
  return createMessage({ kind: 'open_id', id: userId }, 'interactive', JSON.stringify(buildInteractiveCardV1({ body: markdown, buttons: [] })));
}

async function requireSendTarget(userId: string, opts?: { advanceRound?: boolean; threadTs?: string }): Promise<SendTarget> {
  let target = resolveSendTarget(userId, opts);
  const lane = decodeLaneUserId(userId);
  if (!target && lane?.threadId) {
    // A persisted topic lane survives restart, unlike its in-memory reply anchor.
    // Recover from that exact thread; never fall back to the group main timeline.
    const epoch = S().accountEpoch;
    const res = await ensureClient().im.v1.message.list({
      params: { container_id_type: 'thread', container_id: lane.threadId,
        sort_type: 'ByCreateTimeDesc', page_size: 50 },
    });
    if (epoch !== S().accountEpoch) throw new Error('Feishu account replaced during anchor recovery');
    ensureClient();
    if (res.code !== 0) throw new Error(`Feishu topic anchor lookup failed: ${res.code}`);
    const anchor = res.data?.items?.find((item) =>
      !item.deleted && item.message_id?.startsWith('om_') &&
      item.thread_id === lane.threadId && item.chat_id === lane.chatId);
    // Inbound may have supplied an anchor while the lookup was in flight.
    target = resolveSendTarget(userId, opts);
    if (!target && anchor?.message_id) {
      pushReplyAnchor(userId, anchor.message_id);
      target = resolveSendTarget(userId, opts);
    }
  }
  if (!target) {
    throw new Error(
      `[feishu/outbound] no reply anchor for topic lane ...${userId.slice(-8)} — message dropped`,
    );
  }
  return target;
}

/** Keep lookup and delivery on the same account across their async boundary. */
async function withSendTarget<T>(
  userId: string,
  opts: { advanceRound?: boolean; threadTs?: string } | undefined,
  send: (target: SendTarget) => Promise<T>,
): Promise<T> {
  return runWithPinnedAccount({ client: ensureClient(), epoch: S().accountEpoch }, async () => {
    const target = await requireSendTarget(userId, opts);
    ensureClient();
    return send(target);
  });
}

export function getBoundClient(): Lark.Client | null {
  return S().client;
}

export function getBoundCreds(): BotCredentials | null {
  return S().creds;
}

interface PinnedAccount {
  client: Lark.Client;
  epoch: number;
}

const pinnedAccount = new AsyncLocalStorage<PinnedAccount>();

/** Run outbound work against the client captured at schedule time. */
export function runWithPinnedAccount<T>(
  pin: PinnedAccount,
  fn: () => Promise<T> | T,
): Promise<T> | T {
  return pinnedAccount.run(pin, fn);
}

/** `true` when no pin is active, or the pin still matches the current account epoch. */
export function isPinnedAccountCurrent(): boolean {
  const pin = pinnedAccount.getStore();
  if (!pin) return true;
  return pin.epoch === S().accountEpoch;
}

function ensureClient(): Lark.Client {
  const pin = pinnedAccount.getStore();
  if (pin) {
    if (pin.epoch !== S().accountEpoch) {
      throw new Error(
        '[feishu/outbound] pinned Feishu account was replaced — send aborted',
      );
    }
    return pin.client;
  }
  const client = S().client;
  if (!client)
    throw new Error('[feishu/outbound] Lark.Client not bound — feishu connection not established');
  return client;
}

// ── basic text ────────────────────────────────────────────────────────────────

export async function sendText(userId: string, text: string, opts?: { threadTs?: string }): Promise<{ messageId: string }> {
  return withSendTarget(userId, opts, (target) => createMessage(target, 'text', JSON.stringify({ text })));
}

/** 直接回复某条消息(非 owner 群 @ 的礼貌回应等 — 不走 lane 锚点)。 */
export async function replyText(
  replyToMessageId: string,
  text: string,
): Promise<{ messageId: string }> {
  return createMessage(
    { kind: 'reply', messageId: replyToMessageId },
    'text',
    JSON.stringify({ text }),
  );
}

// ── openThread (群主流 @ 开话题) ────────────────────────────────────────────
// 开话题对同一触发消息幂等: 飞书可能重复投递同一条群主流 @ 事件(WS 重连等),
// 不合并就会同一条消息开出多个话题、重复 agent 回答。进行中的请求共享同一
// promise, 已完成的按 TTL 短缓存 — 重投事件直接复用同一个话题。缓存键是
// 触发消息 id(平台内唯一), 不随 unbindClient 清空(换代不会重放别的消息 id)。

/** 开话题结果 — 调用方按状态决定路由与降级(见 openThread 文档)。 */
export type OpenThreadOutcome =
  /** 话题已开, 开场白 messageId + threadId 都是话题内合法锚点/身份。 */
  | { kind: 'opened'; messageId: string; threadId: string }
  /** 无可确认已发出的开场白(API 失败/无 id, 或已成功撤回) — 可降级群 lane。 */
  | { kind: 'degraded' }
  /**
   * 开场白已发出但 thread_id 恢复失败、撤回也失败 — 开场白孤立在话题里。
   * 降级群 lane 会一边留着「思考中」的开场白卡、一边把回答刷进群主流;
   * 调用方应回复开场白(落回话题)说明失败并放弃本轮, 而不是降级。
   */
  | { kind: 'orphaned'; openerMessageId: string }
  /**
   * reply 在超时/断线后用同一 uuid 有界重试仍拿不到回执 — 服务端可能已经
   * 发出「思考中」卡。不能降级群 lane(会刷屏 + 留卡), 调用方应放弃本轮
   * 并 evict 缓存, 让重投再用同一 uuid 取回原 message_id/thread_id。
   */
  | { kind: 'unconfirmed' };

const OPEN_THREAD_DEDUP_TTL_MS = 10 * 60_000;
const OPEN_THREAD_DEDUP_MAX_ENTRIES = 200;
/** 超时/断线后用同一 uuid 再打 reply 的次数(不含首次)。 */
const OPEN_THREAD_UNCERTAIN_RETRIES = 2;

function pruneOpenThreadDedup(): void {
  const now = Date.now();
  for (const [triggerId, entry] of S().openThreadByTrigger) {
    if (now - entry.ts > OPEN_THREAD_DEDUP_TTL_MS) S().openThreadByTrigger.delete(triggerId);
  }
  while (S().openThreadByTrigger.size > OPEN_THREAD_DEDUP_MAX_ENTRIES) {
    let oldestKey: string | undefined;
    let oldestTs = Number.POSITIVE_INFINITY;
    for (const [triggerId, entry] of S().openThreadByTrigger) {
      if (entry.ts < oldestTs) {
        oldestTs = entry.ts;
        oldestKey = triggerId;
      }
    }
    if (oldestKey === undefined) break;
    S().openThreadByTrigger.delete(oldestKey);
  }
}

/**
 * 以 reply_in_thread 回复触发消息, 用它作为根开一个新话题。开场白**就是
 * 本轮流式卡**: 发一张「思考中」占位卡片(message 可被 patch 升级成正文),
 * 之后流式卡直接 patch 它 — 话题里不会多出一条「开个话题聊这条」的占位
 * 消息, 第一条可见内容就是答案本身(streamingText.start 经
 * claimPatchableOpener 认领)。返回 opened / degraded / orphaned / unconfirmed
 * (见类型注释)。同一触发消息并发/重复调用
 * 共享同一次开话题 — 防飞书重投事件开出多个话题(含进程重启: uuid 走服务端
 * 去重)。
 *
 * 失败语义: API 失败或响应缺 message_id → degraded(没有可确认已发出的开场白);
 * 响应有 message_id 但缺 thread_id → 用 message.get 补查话题 id; 查不到就
 * 撤回开场白 — 撤回成功 → degraded, 撤回失败(含业务错误码)→ orphaned。
 * reply 超时/断线等不确定错误 → 同一 uuid 有界重试取回原消息; 仍无回执 →
 * unconfirmed(不降级)。任何状态都不出现「开场白可见但回答进群主流」。
 */
export function openThread(replyToMessageId: string): Promise<OpenThreadOutcome> {
  const now = Date.now();
  const cached = S().openThreadByTrigger.get(replyToMessageId);
  if (cached && now - cached.ts <= OPEN_THREAD_DEDUP_TTL_MS) return cached.promise;

  const promise = doOpenThread(replyToMessageId).then((outcome) => {
    // unconfirmed 不是稳定结论: 清掉缓存, 重投再用同一 uuid 取回原消息。
    if (outcome.kind === 'unconfirmed') S().openThreadByTrigger.delete(replyToMessageId);
    return outcome;
  });
  S().openThreadByTrigger.set(replyToMessageId, { ts: now, promise });
  pruneOpenThreadDedup();
  return promise;
}

/**
 * 移除某触发消息的开话题结果缓存 — 只在「放弃本轮、期待重投重试」的路径
 * 调用(连接换代丢弃时与入站认领释放配套)。不 evict 的话, 重投会复用旧连接
 * 上的 degraded/orphaned 结果(旧客户端已 unbind, 补查/撤回必然失败), 而不是
 * 用新客户端重试 API。孤儿提示失败的重试路径不 evict — 复用 orphaned 结果
 * 重试提示正是设计意图。
 */
export function evictOpenThreadOutcome(replyToMessageId: string): void {
  S().openThreadByTrigger.delete(replyToMessageId);
}

/**
 * 用指定 client 撤回 bot 自己发的消息 — patch/替换失败路径把撤回 pin 到
 * 触发账号: 中途换凭证时不得拿新账号的 client 删除旧账号的开场白。
 */
/** 飞书 REST 2xx 仍可能带非零业务码; SDK 对此不抛。缺省或 0 才算成功。 */
function feishuBusinessRejectReason(res: { code?: number; msg?: string }): string | null {
  if (res.code === undefined || res.code === 0) return null;
  return res.msg ? `${res.msg} (code=${res.code})` : `code=${res.code}`;
}

export async function recallOwnMessageWith(
  c: Lark.Client,
  messageId: string,
): Promise<boolean> {
  const log = getLog();
  try {
    const res = await c.im.v1.message.delete({ path: { message_id: messageId } });
    const rejected = feishuBusinessRejectReason(res);
    if (rejected) {
      log.warn(`[feishu/outbound] recallOwnMessage rejected: ${rejected}`);
      return false;
    }
    return true;
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.warn(`[feishu/outbound] recallOwnMessage failed: ${msg}`);
    return false;
  }
}

/**
 * 撤回 bot 自己发的消息(孤儿开场白卡重试耗尽后的最后兜底)。返回是否成功;
 * 失败只 log — 撤回成功用户看到干净群主流而不是永久「思考中」卡, 撤回失败
 * 说明故障仍未恢复, 已无更进一步的兜底手段。
 */
export async function recallOwnMessage(messageId: string): Promise<boolean> {
  return recallOwnMessageWith(ensureClient(), messageId);
}

/** reply 抛错是否「结果不确定」: 请求可能已在服务端落地, 不能立刻假定没发。 */
function isUncertainOpenThreadError(err: unknown): boolean {
  const name = err instanceof Error ? err.name : '';
  const msg = err instanceof Error ? err.message : String(err);
  const code =
    err && typeof err === 'object' && 'code' in err ? String((err as { code?: unknown }).code) : '';
  const hay = `${name} ${code} ${msg}`.toLowerCase();
  return /timeout|timed?\s*out|etimedout|econnreset|econnrefused|econnaborted|enotfound|eai_again|epipe|socket hang up|network|fetch failed|aborted|und_err/.test(
    hay,
  );
}

async function replyOpenThread(
  c: Lark.Client,
  replyToMessageId: string,
): Promise<{ messageId: string; threadId: string } | 'degraded' | 'unconfirmed'> {
  const log = getLog();
  const maxAttempts = 1 + OPEN_THREAD_UNCERTAIN_RETRIES;
  for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
    try {
      const res = await c.im.v1.message.reply({
        path: { message_id: replyToMessageId },
        data: {
          content: JSON.stringify(buildMarkdownCardV2(transportMessages.streaming.randomThinking())),
          msg_type: 'interactive',
          reply_in_thread: true,
          // 服务端幂等键: 同一触发消息重复开话题(重投事件、进程重启后重放)时,
          // 飞书按 uuid 去重(1 小时内同 uuid 至多发一条, 重复调用返回原消息
          // id), 不会开出第二个话题。超时后用同一 uuid 重试即可取回原消息。
          uuid: replyToMessageId,
        },
      });
      const messageId = res.data?.message_id ?? '';
      const threadId = res.data?.thread_id ?? '';
      if (!messageId) {
        log.warn(
          '[feishu/outbound] openThread: no message_id in response — nothing provably sent',
        );
        return 'degraded';
      }
      return { messageId, threadId };
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      const uncertain = isUncertainOpenThreadError(err);
      if (uncertain && attempt < maxAttempts) {
        log.warn(
          `[feishu/outbound] openThread reply uncertain (retry ${attempt}/${OPEN_THREAD_UNCERTAIN_RETRIES} with same uuid): ${msg}`,
        );
        continue;
      }
      if (uncertain) {
        log.warn(
          `[feishu/outbound] openThread reply unconfirmed after ${maxAttempts} attempts — not degrading: ${msg}`,
        );
        return 'unconfirmed';
      }
      log.warn(`[feishu/outbound] openThread failed (fallback to group lane): ${msg}`);
      return 'degraded';
    }
  }
  return 'unconfirmed';
}

async function doOpenThread(replyToMessageId: string): Promise<OpenThreadOutcome> {
  const log = getLog();
  // 全程 pin 到触发时的 client: reply / message.get / message.delete 都用
  // 同一个实例。中途换账号(unbindClient + bindClient 新凭证)时, 补查与
  // 撤回仍走创建开场白的那条连接 — 不会经新账号的 client 操作旧消息。
  let c: Lark.Client;
  try {
    c = ensureClient();
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.warn(`[feishu/outbound] openThread failed (fallback to group lane): ${msg}`);
    return { kind: 'degraded' };
  }
  const replied = await replyOpenThread(c, replyToMessageId);
  if (replied === 'degraded') return { kind: 'degraded' };
  if (replied === 'unconfirmed') return { kind: 'unconfirmed' };
  const { messageId, threadId } = replied;
  if (threadId) return { kind: 'opened', messageId, threadId };
  // 部分成功: 开场白已发出但响应缺 thread_id — 补查消息详情恢复话题 id。
  const recovered = await tryFetchMessageThreadId(c, messageId);
  if (recovered) return { kind: 'opened', messageId, threadId: recovered };
  // 恢复不了: 撤回开场白再降级, 避免「思考中」的开场白卡留在群里误导。
  try {
    const del = await c.im.v1.message.delete({
      path: { message_id: messageId },
    });
    // SDK 对业务错误不抛异常, 2xx 响应也可能带非零 code — 只有 code 缺省
    // 或为 0 才算撤回成功, 否则开场白卡仍在群里, 降级会制造误导组合。
    if (del.code !== undefined && del.code !== 0) {
      log.error(
        `[feishu/outbound] openThread: opener recall rejected (code=${del.code}) — opener orphaned: ${del.msg ?? ''}`,
      );
      return { kind: 'orphaned', openerMessageId: messageId };
    }
    log.warn(
      '[feishu/outbound] openThread: thread_id unrecoverable — opener recalled, fallback to group lane',
    );
    return { kind: 'degraded' };
  } catch (err) {
    // 撤回也失败: 开场白卡孤立在话题里 — 宁可让调用方放弃本轮(回复开场白
    // 说明失败), 也不能降级群 lane 制造「开场白可见 + 回答刷群主流」。
    const msg = err instanceof Error ? err.message : String(err);
    log.error(
      `[feishu/outbound] openThread: thread_id unrecoverable and opener recall failed — opener orphaned: ${msg}`,
    );
    return { kind: 'orphaned', openerMessageId: messageId };
  }
}

/** 用 message.get 补查开场白消息的 thread_id(部分成功恢复);失败返回 ''。 */
async function tryFetchMessageThreadId(c: Lark.Client, messageId: string): Promise<string> {
  const log = getLog();
  try {
    const res = await c.im.v1.message.get({ path: { message_id: messageId } });
    return res.data?.items?.[0]?.thread_id ?? '';
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.warn(`[feishu/outbound] openThread: thread_id recovery via message.get failed: ${msg}`);
    return '';
  }
}

// ── reactions (used by host orchestrator to ack user msgs) ────────────────────

/**
 * 给消息加一个表情回复,返回 reaction_id 供后续 removeReaction 使用。
 * - 失败 swallow,返 null(emoji ack 是 nice-to-have,不应阻塞主流程)。
 * - 飞书规则:只有原始添加者(此处是 bot 自己)能删除该 reaction,所以
 *   reaction_id 必须配对持有,跨进程/重启不可恢复 → 调用方负责短期持有。
 */
export async function addReaction(messageId: string, emojiType: string): Promise<string | null> {
  const log = getLog();
  try {
    const res = await ensureClient().im.v1.messageReaction.create({
      path: { message_id: messageId },
      data: { reaction_type: { emoji_type: emojiType } },
    });
    return (res as { data?: { reaction_id?: string } }).data?.reaction_id ?? null;
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.warn(`[feishu/outbound] addReaction failed (non-fatal): ${msg}`);
    return null;
  }
}

/**
 * 撤销之前 addReaction 返回的 reaction_id 对应的表情。
 * 失败向调用方抛出,让状态切换保留原 token;终态由编排层尽力清理。
 */
export async function removeReaction(messageId: string, reactionId: string): Promise<void> {
  try {
    const response = await ensureClient().im.v1.messageReaction.delete({
      path: { message_id: messageId, reaction_id: reactionId },
    });
    const rejected = feishuBusinessRejectReason(response);
    if (rejected) throw new Error(`removeReaction failed: ${rejected}`);
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    getLog().warn(`[feishu/outbound] removeReaction failed (non-fatal): ${msg}`);
    throw err;
  }
}

// ── interactive cards ─────────────────────────────────────────────────────────

// ── card lane registry (发卡登记 messageId → lane) ────────────────────────────
// 卡片回调(card.action.trigger)的 context 只带 open_message_id / open_chat_id,
// 不带话题 thread_id — 编排层按 (bot, senderId) 记 /ctr 锁与接管 binding, 消息
// 侧 senderId 是群话题 lane(g/{chatId}/{threadId}), 回调侧只有 operator.open_id,
// 键对不上会让 /ctr 锁永远清不掉。发往 lane 的卡在发送成功后把 messageId 登记
// 回发卡 lane, 回调时反查归一成同一条 lane;私聊卡(open_id)与改投 owner DM 的卡
// 不登记, 回调保持 open_id。飞书卡片回调有效期 30 天, 过期条目不会再有点击,
// 按 31 天 TTL + 上限淘汰;unbindClient 清空(卡片回调不跨连接认领旧卡)。

const CARD_LANE_TTL_MS = 31 * 24 * 60 * 60 * 1000;
const CARD_LANE_MAX_ENTRIES = 512;

function pruneCardLanes(): void {
  const now = Date.now();
  for (const [messageId, entry] of S().cardLanes) {
    if (now - entry.ts > CARD_LANE_TTL_MS) S().cardLanes.delete(messageId);
  }
  while (S().cardLanes.size > CARD_LANE_MAX_ENTRIES) {
    let oldestKey: string | undefined;
    let oldestTs = Number.POSITIVE_INFINITY;
    for (const [messageId, entry] of S().cardLanes) {
      if (entry.ts < oldestTs) {
        oldestTs = entry.ts;
        oldestKey = messageId;
      }
    }
    if (oldestKey === undefined) break;
    S().cardLanes.delete(oldestKey);
  }
}

/** 发卡成功后在 lane 通道登记 card messageId → laneUserId;私聊发送不登记。 */
/** 登记交互卡的发卡 lane(按钮回调 resolveCardLane 反查用)。 */
export function registerCardLane(userId: string, messageId: string): void {
  if (!decodeLaneUserId(userId)) return;
  S().cardLanes.set(messageId, { ts: Date.now(), lane: userId });
  pruneCardLanes();
}

/**
 * 按卡片回调的 open_message_id 反查发卡 lane。chatId 兜底比对防串(卡在话题里
 * 时 open_chat_id 是群 id, 与 lane 的 chatId 恒一致)。查不到返回 null — 私聊卡、
 * 改投 DM 卡或已过期淘汰的条目, 回调 senderId 保持 operator.open_id。
 */
export function resolveCardLane(messageId: string, chatId: string): string | null {
  pruneCardLanes();
  const entry = S().cardLanes.get(messageId);
  if (!entry) return null;
  const lane = decodeLaneUserId(entry.lane);
  if (!lane || lane.chatId !== chatId) return null;
  return entry.lane;
}

export async function sendInteractive(
  userId: string,
  spec: InteractiveCardSpec,
  opts?: { deliverToOwnerDm?: boolean; ownerDmNote?: string; threadTs?: string },
): Promise<{ messageId: string }> {
  // 授权卡改投宿主私聊(群 lane 专用语义): 群里的授权卡只有 owner 能答且
  // 消不掉。owner 未知时保持原 lane 投递, 不吞掉这次交互(telegram 同口径)。
  const owner = ownerGuard.firstAllowed();
  if (opts?.deliverToOwnerDm && decodeLaneUserId(userId) && owner) {
    const dmSpec: InteractiveCardSpec = opts.ownerDmNote
      ? { ...spec, body: `${opts.ownerDmNote}\n\n${spec.body}` }
      : spec;
    const card = buildInteractiveCardV1(dmSpec);
    return createMessage({ kind: 'open_id', id: owner }, 'interactive', JSON.stringify(card));
  }
  const card = buildInteractiveCardV1(spec);
  const result = await withSendTarget(userId, opts, (target) => createMessage(
    target,
    'interactive',
    JSON.stringify(card),
  ));
  registerCardLane(userId, result.messageId);
  return result;
}

export async function updateInteractive(
  messageId: string,
  spec: InteractiveCardSpec,
): Promise<void> {
  const card = buildInteractiveCardV1(spec);
  const res = await ensureClient().im.v1.message.patch({
    path: { message_id: messageId },
    data: { content: JSON.stringify(card) },
  });
  const rejected = feishuBusinessRejectReason(res);
  if (rejected) {
    throw new Error(`updateInteractive rejected: ${rejected}`);
  }
}

// ── raw card patch (used by streamingText for v2 markdown patching) ───────────

export async function patchCardRaw(messageId: string, cardJson: unknown): Promise<void> {
  const res = await ensureClient().im.v1.message.patch({
    path: { message_id: messageId },
    data: { content: JSON.stringify(cardJson) },
  });
  const rejected = feishuBusinessRejectReason(res);
  if (rejected) {
    throw new Error(`patchCardRaw rejected: ${rejected}`);
  }
}

/**
 * Send a brand-new card (used by streamingText to mint the initial message).
 * 流式建卡是每个 agent 回合的首条出站 — 群 lane 在此领取新的回挂锚点。
 */
export async function sendCardRaw(
  userId: string,
  cardJson: unknown,
  opts?: { threadTs?: string; advanceRound?: boolean },
): Promise<{ messageId: string }> {
  return withSendTarget(userId, { advanceRound: true, ...opts }, (target) => createMessage(
    target,
    'interactive',
    JSON.stringify(cardJson),
  ));
}

/** Send a terminal-output mirror directly to a parent group main timeline. */
export async function sendCardToChat(
  chatId: string,
  cardJson: unknown,
  uuid: string,
): Promise<{ messageId: string }> {
  return createMessage(
    { kind: 'chat_id', id: chatId },
    'interactive',
    JSON.stringify(cardJson),
    uuid,
  );
}

// ── file send ────────────────────────────────────────────────────────────────

const FEISHU_IMAGE_EXTS = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp']);

function isFeishuImageExt(absPath: string): boolean {
  return FEISHU_IMAGE_EXTS.has(path.extname(absPath).toLowerCase());
}

function inferFeishuFileType(
  absPath: string,
): 'opus' | 'mp4' | 'pdf' | 'doc' | 'xls' | 'ppt' | 'stream' {
  const ext = path.extname(absPath).toLowerCase();
  if (ext === '.opus') return 'opus';
  if (ext === '.mp4' || ext === '.mov') return 'mp4';
  if (ext === '.pdf') return 'pdf';
  if (['.doc', '.docx'].includes(ext)) return 'doc';
  if (['.xls', '.xlsx'].includes(ext)) return 'xls';
  if (['.ppt', '.pptx'].includes(ext)) return 'ppt';
  return 'stream';
}

/** A Feishu upload reference that can be sent again without reading the source file. */
export interface ReusableFeishuFileMessage {
  msgType: 'file' | 'image';
  content: string;
}

export interface FeishuUploadedFileSource {
  realPath: string;
  /** Exact decimal identities; strings avoid truncating 64-bit Windows file IDs. */
  dev: string;
  ino: string;
  /**
   * Compatibility-only diagnostic. Parent-chat authorization ignores caller-
   * supplied ancestry and walks from the live pinned root handle instead.
   */
  ancestors: ReadonlyArray<{ dev: string; ino: string }>;
}

export interface FeishuSendFileResult extends SendFileResult {
  reusableMessage?: ReusableFeishuFileMessage;
  /** Identity of the bytes that were uploaded; authorizes parent-chat key reuse. */
  uploadedSource?: FeishuUploadedFileSource;
}

export async function sendFile(
  userId: string,
  absPath: string,
  displayName?: string,
  opts?: { threadTs?: string },
): Promise<FeishuSendFileResult> {
  try {
    return await withSendTarget(userId, opts, (target) => sendFileToTarget(target, absPath, displayName));
  } catch {
    getLog().error('[feishu/outbound] sendFile: target or delivery unavailable');
    return { ok: false, reason: 'SEND_FAIL' };
  }
}

/** Re-send an already uploaded terminal-output file to a parent group main timeline. */
export async function sendFileToChat(
  chatId: string,
  message: ReusableFeishuFileMessage,
  uuid: string,
): Promise<SendFileResult> {
  try {
    const res = await createMessage(
      { kind: 'chat_id', id: chatId },
      message.msgType,
      message.content,
      uuid,
    );
    return { ok: true, messageId: res.messageId };
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    getLog().error(`[feishu/outbound] sendFileToChat SEND_FAIL: ${msg}`);
    return { ok: false, reason: 'SEND_FAIL' };
  }
}

interface OutboundFileSource {
  absPath: string;
  size: number;
  createReadStream(): fs.ReadStream;
}

function isSyntheticFdPath(resolved: string): boolean {
  const normalized = resolved.replaceAll('\\', '/');
  return /(?:^|\/)(?:proc\/self\/fd|dev\/fd)\/\d+$/.test(normalized);
}

function realPathIfInode(
  candidate: string,
  identity: { dev: string; ino: string },
): string | null {
  try {
    const realPath = fs.realpathSync.native(candidate);
    if (isSyntheticFdPath(realPath)) return null;
    const leaf = fs.statSync(realPath, { bigint: true });
    if (String(leaf.dev) === identity.dev && String(leaf.ino) === identity.ino) return realPath;
  } catch {
    /* candidate no longer names this inode */
  }
  return null;
}

function runHandlePathHelper(
  command: string,
  args: readonly string[],
  stdin: number | 'ignore',
  stderr: number | 'ignore' = 'ignore',
  env?: NodeJS.ProcessEnv,
): Promise<Buffer | null> {
  return new Promise((resolve) => {
    let child: ReturnType<typeof spawn>;
    try {
      child = spawn(command, args, {
        stdio: [stdin, 'pipe', stderr],
        windowsHide: true,
        ...(env ? { env } : {}),
      });
    } catch {
      resolve(null);
      return;
    }

    let settled = false;
    const finish = (value: Buffer | null): void => {
      if (settled) return;
      settled = true;
      clearTimeout(timer);
      resolve(value);
    };
    const chunks: Buffer[] = [];
    let total = 0;
    child.stdout?.on('data', (chunk: Buffer | string) => {
      const next = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
      total += next.length;
      if (total > HANDLE_PATH_HELPER_MAX_BYTES) {
        child.stdout?.destroy();
        child.kill();
        finish(null);
        return;
      }
      chunks.push(next);
    });
    child.once('error', () => finish(null));
    child.once('close', (code) => finish(code === 0 ? Buffer.concat(chunks) : null));

    const timer = setTimeout(() => {
      child.stdout?.destroy();
      child.kill();
      finish(null);
    }, HANDLE_PATH_HELPER_TIMEOUT_MS);
    timer.unref?.();
  });
}

function windowsPowerShellPath(): string | null {
  const systemRoot = process.env.SystemRoot ?? process.env.WINDIR;
  if (!systemRoot || !path.win32.isAbsolute(systemRoot)) return null;
  const executable = path.win32.join(
    systemRoot,
    'System32',
    'WindowsPowerShell',
    'v1.0',
    'powershell.exe',
  );
  try {
    return fs.statSync(executable).isFile() ? executable : null;
  } catch {
    return null;
  }
}

function normalizeWindowsHandlePath(raw: string): string | null {
  const value = raw.replace(/^\uFEFF/, '');
  if (!value || /[\0\r\n]/.test(value)) return null;
  const extendedPrefix = '\\\\?\\';
  if (!value.startsWith(extendedPrefix)) return null;
  const unprefixed = value.slice(extendedPrefix.length);
  if (unprefixed.startsWith('UNC\\')) return `\\\\${unprefixed.slice(4)}`;
  return /^[A-Za-z]:\\/.test(unprefixed) ? unprefixed : null;
}

async function windowsHandlePath(fd: number): Promise<string | null> {
  const powershell = windowsPowerShellPath();
  if (!powershell) return null;
  const output = await runHandlePathHelper(
    powershell,
    ['-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', WINDOWS_HANDLE_PATH_COMMAND],
    fd,
  );
  return output ? normalizeWindowsHandlePath(output.toString('utf8')) : null;
}

function decodeLsofFileName(raw: string): string {
  if (!raw.includes('\\')) return raw;
  const bytes: number[] = [];
  for (let i = 0; i < raw.length; ) {
    if (raw.startsWith('\\x', i) && /^[0-9a-fA-F]{2}/.test(raw.slice(i + 2, i + 4))) {
      bytes.push(Number.parseInt(raw.slice(i + 2, i + 4), 16));
      i += 4;
      continue;
    }
    const code = raw.charCodeAt(i);
    if (code > 255) {
      bytes.push(...Buffer.from(raw[i]!, 'utf8'));
    } else {
      bytes.push(code);
    }
    i += 1;
  }
  return Buffer.from(bytes).toString('utf8');
}

async function darwinHandlePath(fd: number): Promise<string | null> {
  const output = await runHandlePathHelper(
    '/usr/sbin/lsof',
    ['-w', '-a', '-p', String(process.pid), '-d', String(fd), '-F0n'],
    'ignore',
    'ignore',
    { ...process.env, LANG: 'en_US.UTF-8', LC_ALL: 'en_US.UTF-8' },
  );
  if (!output) return null;
  for (const rawField of output.toString('utf8').split('\0')) {
    const field = rawField.startsWith('\n') ? rawField.slice(1) : rawField;
    if (field.startsWith('n') && field.length > 1) {
      return decodeLsofFileName(field.slice(1));
    }
  }
  return null;
}

/**
 * Native canonical path for the opened handle. A later lookup of `absPath` is
 * a separate operation that can be retargeted. Linux binds through procfs;
 * macOS asks `lsof` for the live vnode path; Windows passes the opened handle
 * to a fixed PowerShell/GetFinalPathNameByHandle helper. Any unavailable or
 * unverifiable helper fails closed.
 */
export async function attestedRealPath(
  fd: number,
  identity: { dev: string; ino: string },
): Promise<string> {
  const candidate =
    process.platform === 'linux'
      ? `/proc/self/fd/${fd}`
      : process.platform === 'darwin'
        ? await darwinHandlePath(fd)
        : process.platform === 'win32'
          ? await windowsHandlePath(fd)
          : null;
  return candidate ? (realPathIfInode(candidate, identity) ?? '') : '';
}

function validContainmentSegments(segments: readonly string[]): boolean {
  if (segments.length === 0) return false;
  return segments.every(
    (segment) =>
      segment.length > 0 &&
      segment !== '.' &&
      segment !== '..' &&
      !segment.includes('\0') &&
      !segment.includes('/'),
  );
}

function sameOpenFile(leftFd: number, rightFd: number): boolean {
  try {
    const left = fs.fstatSync(leftFd, { bigint: true });
    const right = fs.fstatSync(rightFd, { bigint: true });
    if (!left.isFile() || !right.isFile() || left.ino === 0n || right.ino === 0n) return false;
    return left.dev === right.dev && left.ino === right.ino;
  } catch {
    return false;
  }
}

function linuxHandleContainment(
  sourceFd: number,
  rootFd: number,
  segments: readonly string[],
): boolean {
  if (!fs.constants.O_NOFOLLOW || !validContainmentSegments(segments)) return false;
  const opened: number[] = [];
  let parentFd = rootFd;
  try {
    for (let index = 0; index < segments.length; index += 1) {
      const directory = index < segments.length - 1;
      let flags = fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW;
      if (fs.constants.O_NONBLOCK) flags |= fs.constants.O_NONBLOCK;
      if (directory && fs.constants.O_DIRECTORY) flags |= fs.constants.O_DIRECTORY;
      const fd = fs.openSync(`/proc/self/fd/${parentFd}/${segments[index]}`, flags);
      opened.push(fd);
      const stat = fs.fstatSync(fd, { bigint: true });
      if (directory) {
        if (!stat.isDirectory()) return false;
        parentFd = fd;
      } else if (!sameOpenFile(fd, sourceFd)) {
        return false;
      }
    }
    return true;
  } catch {
    return false;
  } finally {
    for (const fd of opened.reverse()) {
      try {
        fs.closeSync(fd);
      } catch {
        /* best-effort close */
      }
    }
  }
}

async function windowsHandleContainment(
  sourceFd: number,
  rootFd: number,
  segments: readonly string[],
): Promise<boolean> {
  if (!validContainmentSegments(segments) || segments.some((segment) => segment.includes('\\'))) {
    return false;
  }
  const powershell = windowsPowerShellPath();
  if (!powershell) return false;
  const encodedSegments = Buffer.from(JSON.stringify(segments), 'utf8').toString('base64');
  const output = await runHandlePathHelper(
    powershell,
    [
      '-NoLogo',
      '-NoProfile',
      '-NonInteractive',
      '-EncodedCommand',
      WINDOWS_HANDLE_CONTAINMENT_COMMAND,
    ],
    rootFd,
    sourceFd,
    { ...process.env, CINDY_HANDLE_SEGMENTS: encodedSegments },
  );
  return output?.toString('utf8') === '1';
}

async function darwinHandleContainment(
  sourceFd: number,
  rootFd: number,
  segments: readonly string[],
): Promise<boolean> {
  if (!validContainmentSegments(segments)) return false;
  const output = await runHandlePathHelper(
    '/usr/bin/perl',
    ['-e', DARWIN_HANDLE_CONTAINMENT_SCRIPT, '--', ...segments],
    rootFd,
    sourceFd,
    {},
  );
  return output?.toString('utf8') === '1';
}

/**
 * Object-chain containment proof. `sourceFd` and `rootFd` stay open throughout
 * the check; every child is opened relative to the preceding directory handle
 * with symlink/reparse traversal disabled, then the final handle is compared
 * to `sourceFd`. No absolute path is re-resolved by the helper.
 */
export async function attestOpenFileWithinDirectory(
  sourceFd: number,
  rootFd: number,
  segments: readonly string[],
): Promise<boolean> {
  if (process.platform === 'linux') {
    return linuxHandleContainment(sourceFd, rootFd, segments);
  }
  if (process.platform === 'darwin') {
    return darwinHandleContainment(sourceFd, rootFd, segments);
  }
  if (process.platform === 'win32') {
    return windowsHandleContainment(sourceFd, rootFd, segments);
  }
  return false;
}

async function sendFileToTarget(
  target: SendTarget,
  absPath: string,
  displayName?: string,
  uuid?: string,
): Promise<FeishuSendFileResult> {
  const c = ensureClient();

  let handle: FileHandle;
  try {
    handle = await openFile(absPath, 'r');
  } catch {
    return { ok: false, reason: 'NOT_FOUND' };
  }

  try {
    const stat = await handle.stat({ bigint: true });
    const identity = { dev: String(stat.dev), ino: String(stat.ino) };
    const realPath = await attestedRealPath(handle.fd, identity);
    const result = await sendFileSourceToTarget(
      c,
      target,
      {
        absPath,
        size: Number(stat.size),
        createReadStream: () => handle.createReadStream({ autoClose: false }),
      },
      displayName,
      uuid,
    );
    if (result.ok) {
      result.uploadedSource = {
        realPath,
        ...identity,
        ancestors: [],
      };
    }
    return result;
  } finally {
    await handle.close().catch(() => undefined);
  }
}

async function sendFileSourceToTarget(
  c: Lark.Client,
  target: SendTarget,
  source: OutboundFileSource,
  displayName?: string,
  uuid?: string,
): Promise<FeishuSendFileResult> {
  const log = getLog();
  const baseName = path.basename(source.absPath);
  const showName = displayName?.length ? displayName : baseName;

  if (source.size === 0) return { ok: false, reason: 'EMPTY' };
  if (source.size > FEISHU_FILE_SIZE_LIMIT) return { ok: false, reason: 'TOO_LARGE' };

  // Image fast-path: if the file is a feishu-supported image type and within
  // the image-msg size cap, send as msg_type:image so it previews inline.
  if (isFeishuImageExt(source.absPath) && source.size <= FEISHU_IMAGE_MAX_BYTES) {
    return sendImageMessage(c, target, source.createReadStream, uuid);
  }

  // 1. Upload to obtain file_key.
  let fileKey: string;
  try {
    const fileType = inferFeishuFileType(source.absPath);
    const res = await c.im.file.create({
      data: {
        file_type: fileType,
        file_name: showName,
        file: source.createReadStream(),
      },
    });
    const key = (res as { file_key?: string } | null)?.file_key;
    if (!key) return { ok: false, reason: 'UPLOAD_FAIL' };
    fileKey = key;
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.error(`[feishu/outbound] sendFile UPLOAD_FAIL: ${msg}`);
    return { ok: false, reason: 'UPLOAD_FAIL' };
  }

  // 2. Send message referencing file_key.
  try {
    const content = JSON.stringify({ file_key: fileKey });
    const res = await createMessage(target, 'file', content, uuid);
    return {
      ok: true,
      messageId: res.messageId,
      reusableMessage: { msgType: 'file', content },
    };
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.error(`[feishu/outbound] sendFile SEND_FAIL: ${msg}`);
    return { ok: false, reason: 'SEND_FAIL' };
  }
}

/**
 * Upload a local image file to feishu and return its `image_key`. Used by
 * streamingText to inline `xdt-image://...` references as feishu `img`
 * elements inside an interactive card. Caller is responsible for size /
 * format checks; we fail-soft (log + null) on any error so a single bad image
 * doesn't break the whole card patch.
 *
 * 10 MB cap (feishu image-message limit). Use `sendFile` for larger blobs.
 */
export async function uploadImage(absPath: string): Promise<string | null> {
  const log = getLog();
  try {
    if (!fs.existsSync(absPath)) {
      log.warn(`[feishu/outbound] uploadImage NOT_FOUND ${absPath}`);
      return null;
    }
    const stat = fs.statSync(absPath);
    if (stat.size === 0 || stat.size > FEISHU_IMAGE_MAX_BYTES) {
      log.warn(`[feishu/outbound] uploadImage size ineligible ${stat.size} for ${absPath}`);
      return null;
    }
    const res = await ensureClient().im.v1.image.create({
      data: {
        image_type: 'message',
        image: fs.createReadStream(absPath),
      },
    });
    const key = (res as { image_key?: string }).image_key;
    return key ?? null;
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.warn(`[feishu/outbound] uploadImage failed: ${msg}`);
    return null;
  }
}

// ── exact reply context ───────────────────────────────────────────────────────

export interface ResolvedReplyMessage {
  replyContext: NonNullable<IMMessageEvent['replyContext']>;
  attachments?: AttachmentRef[];
}

function renderReplyMessageText(
  parsed: ReturnType<typeof parseIncoming>,
): string {
  const parts: string[] = [];
  if (parsed.text) parts.push(parsed.text);
  for (const attachment of parsed.attachments) {
    parts.push(attachment.kind === 'image' ? '[图片]' : `[文件: ${attachment.fileName}]`);
  }
  for (const unsupported of parsed.unsupported) parts.push(`[${unsupported.label}]`);
  return parts.join('\n').trim();
}

function replaceFetchedMentions(
  text: string,
  mentions: Array<{ key: string; name?: unknown }> | undefined,
): string {
  let out = text;
  for (const mention of mentions ?? []) {
    if (!mention.key) continue;
    const rawName = typeof mention.name === 'string' ? mention.name : '';
    const name = rawName.replace(/[\p{Cc}\p{Cf}]/gu, ' ').trim().slice(0, 64);
    out = out.split(mention.key).join(`@${name || 'user'}`);
  }
  return out;
}

/**
 * Fetch a quote (including merge-forward descendants) from the originating chat.
 * Keep resource ownership for the host to download after screening the text.
 */
export async function resolveReplyMessage(
  messageId: string,
  expectedChatId: string,
): Promise<ResolvedReplyMessage | null> {
  const c = S().client;
  if (!c) return null;
  return resolveReplyWithClient(c, messageId, expectedChatId);
}

async function resolveReplyWithClient(
  c: Lark.Client,
  messageId: string,
  expectedChatId: string,
  readBudget = { remaining: 21 },
): Promise<ResolvedReplyMessage | null> {
  const log = getLog();
  try {
    if (readBudget.remaining <= 0) return null;
    readBudget.remaining--;
    const res = await c.im.v1.message.get({
      params: { user_id_type: 'open_id', with_sender_name: true },
      path: { message_id: messageId },
    });
    const rejected = feishuBusinessRejectReason(res);
    if (rejected) {
      log.warn(`[feishu/outbound] reply context fetch rejected: ${rejected}`);
      return null;
    }
    const items = res.data?.items ?? [];
    const root = items.find((item) => item.message_id === messageId);
    if (!root || root.deleted || root.chat_id !== expectedChatId) return null;
    const authorOf = (item: typeof root): string =>
      (item.sender?.sender_name || (item.sender?.sender_type === 'app' ? 'bot' : 'user'))
        .replace(/[\p{Cc}\p{Cf}]/gu, ' ').trim().slice(0, 64);
    const attachments: AttachmentRef[] = [];
    const visited = new Set<string>();
    const render = async (item: typeof root): Promise<string> => {
      if (!item.message_id || item.deleted || visited.has(item.message_id)) return '';
      // The envelope is checked against the triggering chat above. Forwarded
      // children belong through upper_message_id, even when their original chat differs.
      visited.add(item.message_id);
      if (item.msg_type === 'merge_forward') {
        let children = items.filter((child) => child.upper_message_id === item.message_id);
        // Some responses include only the immediate level. Fetch deeper envelopes
        // with the same client; never follow arbitrary message IDs from body text.
        if (!children.length && item !== root && readBudget.remaining > 0) {
          try {
            readBudget.remaining--;
            const nested = await c.im.v1.message.get({
              params: { user_id_type: 'open_id', with_sender_name: true },
              path: { message_id: item.message_id },
            });
            if (!feishuBusinessRejectReason(nested)) {
              const extra = nested.data?.items ?? [];
              for (const child of extra) {
                if (!items.some((known) => known.message_id === child.message_id)) items.push(child);
              }
              children = items.filter((child) => child.upper_message_id === item.message_id);
            }
          } catch {
            // Preserve a visible unreadable marker and all readable siblings.
          }
        }
        const lines: string[] = [];
        for (const child of children) {
          const text = await render(child);
          if (text) lines.push(`[${authorOf(child)}] ${text}`);
        }
        return lines.length ? `[合并转发消息]\n${lines.join('\n')}`
          : readBudget.remaining <= 0 ? '[合并转发消息:读取预算已用完]' : '[合并转发消息:未能读取子消息]';
      }
      const parsed = parseIncoming(item.msg_type ?? '', item.body?.content ?? '');
      attachments.push(...parsed.attachments.map((ref) => ({ ...ref, messageId: item.message_id })));
      return replaceFetchedMentions(renderReplyMessageText(parsed), item.mentions);
    };
    const text = await render(root);
    if (!text) return null;
    return {
      replyContext: {
        author: authorOf(root),
        text,
        ...(root.sender?.sender_type === 'app' ? { isBot: true } : {}),
      },
      ...(attachments.length ? { attachments } : {}),
    };
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.warn(`[feishu/outbound] reply context fetch failed (degraded): ${msg}`);
    return null;
  }
}

// ── group history (context assembly for group lanes) ──────────────────────────

export interface RecentChatMessage {
  messageId: string;
  threadId: string;
  senderName: string;
  senderOpenId: string;
  senderIsBot: boolean;
  text: string;
  /** 图片/文件附件引用(image_key / file_key) — 下载需要配对的 messageId。 */
  attachments: AttachmentRef[];
  createTimeMs: number;
}

export interface ChatHistoryPage {
  /** 页内按时间升序(拉取是倒序的, 返回前翻正)。 */
  messages: RecentChatMessage[];
  /** 还有更早的页时给下一次调用的 page_token; 没有更早历史为 null。 */
  nextPageToken: string | null;
}

/**
 * 按页拉群/话题历史(群 lane 触发时 adapter 拼上下文用)。倒序拉一页, 返回
 * 页内升序。展开转发前按 threadId 筛选; 缺省只保留群主流。
 *
 * 需要「获取群组中所有消息」权限。**失败(权限不足/网络)直接抛错** — 调用方
 * 需要区分「真的没有历史」与「拉取失败」来做降级提示, 不能吞成空数组。
 * 文本抽取复用 parseIncoming; audio/media/sticker 等对上下文无意义的类型跳过。
 */
export async function fetchChatHistoryPage(args: {
  chatId: string;
  threadId?: string;
  pageToken?: string;
  pageSize?: number;
  excludeMessageId?: string;
  /** Mutable per-turn allowance, shared by every page and nested message.get. */
  mergeForwardReadBudget?: { remaining: number };
}): Promise<ChatHistoryPage> {
  const c = S().client;
  if (!c) throw new Error('feishu client not bound');
  const res = await c.im.v1.message.list({
    params: {
      container_id_type: args.threadId ? 'thread' : 'chat',
      container_id: args.threadId ?? args.chatId,
      sort_type: 'ByCreateTimeDesc',
      page_size: Math.min(Math.max(args.pageSize ?? 50, 1), 50),
      with_sender_name: true,
      ...(args.pageToken ? { page_token: args.pageToken } : {}),
    },
  });
  if (res.code !== 0) {
    throw new Error(`im.message.list failed: code=${res.code} msg=${res.msg ?? 'unknown'}`);
  }
  const items = res.data?.items ?? [];
  const out: RecentChatMessage[] = [];
  const readBudget = args.mergeForwardReadBudget ?? { remaining: 20 };
  for (const item of items) {
    if (!item.message_id || item.deleted) continue;
    if (item.message_id === args.excludeMessageId || (item.thread_id ?? '') !== (args.threadId ?? '')) continue;
    const msgType = item.msg_type ?? '';
    const rawContent = item.body?.content ?? '';
    let text = '';
    let attachments: AttachmentRef[] = [];
    if (msgType === 'merge_forward') {
      const exhausted = readBudget.remaining <= 0;
      const resolved = exhausted ? null : await resolveReplyWithClient(c, item.message_id, args.chatId, readBudget);
      text = resolved?.replyContext.text ?? (exhausted ? '[合并转发消息:读取预算已用完]' : '[合并转发消息:读取失败]');
      attachments = resolved?.attachments ?? [];
    } else if (
      msgType === 'text' ||
      msgType === 'post' ||
      msgType === 'image' ||
      msgType === 'file' ||
      msgType === 'interactive' ||
      msgType === 'card'
    ) {
      const parsed = parseIncoming(msgType, rawContent);
      text = parsed.text;
      attachments = parsed.attachments;
    } else {
      continue; // audio/media/sticker 等对上下文无意义, 跳过
    }
    if (!text && attachments.length === 0) continue;
    out.push({
      messageId: item.message_id,
      threadId: item.thread_id ?? '',
      senderName: item.sender?.sender_name ?? '',
      senderOpenId: item.sender?.id_type === 'open_id' ? (item.sender?.id ?? '') : '',
      senderIsBot: item.sender?.sender_type === 'app',
      text,
      attachments,
      createTimeMs: Number(item.create_time ?? 0),
    });
  }
  out.reverse();
  const hasMore = res.data?.has_more === true;
  const nextToken = res.data?.page_token;
  return {
    messages: out,
    nextPageToken: hasMore && nextToken ? nextToken : null,
  };
}

/**
 * 拉群名称(群 lane 会话标题用)。需要「获取群基本信息」权限; 失败/无权限
 * 返回 null — 调用方回落 chatId 后 6 位。bot 拉不到群名的常见原因与群历史
 * 相同: 应用未开权限或未发布版本。
 */
export async function getChatName(chatId: string): Promise<string | null> {
  const log = getLog();
  const c = S().client;
  if (!c) return null;
  try {
    const res = await c.im.v1.chat.get({ path: { chat_id: chatId } });
    if (res.code !== 0) {
      log.warn(`[feishu/outbound] chat.get failed: code=${res.code} msg=${res.msg ?? 'unknown'}`);
      return null;
    }
    return res.data?.name ?? null;
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.warn(`[feishu/outbound] chat.get failed: ${msg}`);
    return null;
  }
}


// ── Agent 直通 Open API(cindy_feishu_bot 轮次内工具) ────────────────────────
// 这些函数不走 lane 锚点 / opener 消费: 它们是 Agent 在轮次里显式操作当前对话的
// 能力(查群信息、成员、置顶、撤回自己的消息、任意 Open API)。目标 chat / message
// 由桌面侧逐轮作用域钉死, 这里只负责把 SDK 返回与业务码折成稳定的错误形态。

/**
 * 飞书 Open API 调用失败(HTTP 4xx/5xx 或 2xx 带非零业务码)。`code` 是飞书业务码
 * (如 99991663 权限不足、230001 无权发消息), `httpStatus` 是 HTTP 状态(业务码拒绝时
 * 为 200)。MCP 层据此给模型稳定的 errorCode, 不把 axios 内部结构透给模型。
 */
export class FeishuApiError extends Error {
  readonly code: number | undefined;
  readonly httpStatus: number | undefined;
  readonly apiMsg: string;
  constructor(args: { code?: number; httpStatus?: number; msg: string }) {
    super(`feishu api ${args.httpStatus ?? ''} code=${args.code ?? '?'} ${args.msg}`.replace(/\s+/g, ' ').trim());
    this.name = 'FeishuApiError';
    this.code = args.code;
    this.httpStatus = args.httpStatus;
    this.apiMsg = args.msg;
  }
}

function toFeishuApiError(err: unknown): FeishuApiError {
  if (err instanceof FeishuApiError) return err;
  const r = (err as { response?: { status?: number; data?: { code?: number; msg?: string } } })?.response;
  if (r && (typeof r.status === 'number' || r.data)) {
    return new FeishuApiError({
      code: typeof r.data?.code === 'number' ? r.data.code : undefined,
      httpStatus: typeof r.status === 'number' ? r.status : undefined,
      msg: r.data?.msg ?? (err instanceof Error ? err.message : String(err)),
    });
  }
  return new FeishuApiError({ msg: err instanceof Error ? err.message : String(err) });
}

/** 2xx 带非零业务码 → 抛 FeishuApiError; 否则原样返回响应体。 */
function unwrapApiResponse<T extends { code?: number; msg?: string }>(res: T): T {
  if (typeof res?.code === 'number' && res.code !== 0) {
    throw new FeishuApiError({ code: res.code, httpStatus: 200, msg: res.msg ?? 'rejected' });
  }
  return res;
}

export type FeishuApiMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';

/**
 * 以本连接 bot 的 tenant_access_token 调任意飞书 Open API。`path` 形如
 * `/open-apis/im/v1/chats/{chat_id}`(SDK 自动拼 Feishu / Lark 域名并带 token)。
 * 返回响应体(含 `code` / `msg` / `data`); HTTP 错误或业务码非零一律抛 FeishuApiError。
 */
export async function callApi(args: {
  method: FeishuApiMethod;
  path: string;
  params?: Record<string, unknown>;
  data?: unknown;
}): Promise<Record<string, unknown>> {
  const c = ensureClient();
  const url = args.path.startsWith('/') ? args.path.slice(1) : args.path;
  try {
    const res = await c.request<Record<string, unknown>>({
      method: args.method,
      url,
      ...(args.params ? { params: args.params } : {}),
      ...(args.data !== undefined ? { data: args.data } : {}),
    });
    return unwrapApiResponse((res ?? {}) as { code?: number; msg?: string }) as Record<string, unknown>;
  } catch (err) {
    throw toFeishuApiError(err);
  }
}

/** 群 / 私聊会话信息(需「获取群信息」权限)。 */
export async function getChatInfo(chatId: string): Promise<Record<string, unknown>> {
  const res = await callApi({ method: 'GET', path: `/open-apis/im/v1/chats/${encodeURIComponent(chatId)}` });
  return (res.data as Record<string, unknown> | undefined) ?? {};
}

export interface FeishuChatMembersPage {
  items: Array<{ memberId: string; name: string }>;
  memberTotal: number | null;
  nextPageToken: string | null;
}

/** 群成员一页(open_id + 名字), 需「获取群成员列表」权限。 */
export async function listChatMembers(
  chatId: string,
  opts?: { pageToken?: string; pageSize?: number },
): Promise<FeishuChatMembersPage> {
  const res = await callApi({
    method: 'GET',
    path: `/open-apis/im/v1/chats/${encodeURIComponent(chatId)}/members`,
    params: {
      member_id_type: 'open_id',
      page_size: String(Math.min(Math.max(opts?.pageSize ?? 100, 1), 100)),
      ...(opts?.pageToken ? { page_token: opts.pageToken } : {}),
    },
  });
  const data = (res.data ?? {}) as {
    items?: Array<{ member_id?: string; name?: string }>;
    member_total?: number;
    has_more?: boolean;
    page_token?: string;
  };
  return {
    items: (data.items ?? [])
      .filter((m) => typeof m.member_id === 'string')
      .map((m) => ({ memberId: m.member_id as string, name: m.name ?? '' })),
    memberTotal: typeof data.member_total === 'number' ? data.member_total : null,
    nextPageToken: data.has_more && data.page_token ? data.page_token : null,
  };
}

/** 读一条消息的原始体(类型 / 发送者 / 内容 JSON 字串 / 时间)。 */
export async function getMessage(messageId: string): Promise<Record<string, unknown> | null> {
  const res = await callApi({ method: 'GET', path: `/open-apis/im/v1/messages/${encodeURIComponent(messageId)}` });
  const items = (res.data as { items?: Array<Record<string, unknown>> } | undefined)?.items;
  return items?.[0] ?? null;
}

/** 群内置顶 / 取消置顶一条消息(bot 需在群里且有「置顶消息」权限)。 */
export async function pinMessage(messageId: string): Promise<void> {
  await callApi({ method: 'POST', path: '/open-apis/im/v1/pins', data: { message_id: messageId } });
}

export async function unpinMessage(messageId: string): Promise<void> {
  await callApi({ method: 'DELETE', path: `/open-apis/im/v1/pins/${encodeURIComponent(messageId)}` });
}

/** 撤回一条消息; 与 recallOwnMessage 不同, 失败抛 FeishuApiError 让调用方看到原因。 */
export async function recallMessage(messageId: string): Promise<void> {
  await callApi({ method: 'DELETE', path: `/open-apis/im/v1/messages/${encodeURIComponent(messageId)}` });
}

/** 以 markdown 卡片回复某条消息(reply 容器, 不走 lane 锚点)。 */
export async function replyMarkdownText(
  replyToMessageId: string,
  markdown: string,
): Promise<{ messageId: string }> {
  return createMessage(
    { kind: 'reply', messageId: replyToMessageId },
    'interactive',
    JSON.stringify(buildMarkdownCardV2(markdown)),
  );
}

async function sendImageMessage(
  c: Lark.Client,
  target: SendTarget,
  createReadStream: () => fs.ReadStream,
  uuid?: string,
): Promise<FeishuSendFileResult> {
  const log = getLog();
  try {
    const upRes = await c.im.v1.image.create({
      data: {
        image_type: 'message',
        image: createReadStream(),
      },
    });
    const imageKey = (upRes as { image_key?: string }).image_key;
    if (!imageKey) return { ok: false, reason: 'UPLOAD_FAIL' };

    const content = JSON.stringify({ image_key: imageKey });
    const res = await createMessage(target, 'image', content, uuid);
    return {
      ok: true,
      messageId: res.messageId,
      reusableMessage: { msgType: 'image', content },
    };
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    log.error(`[feishu/outbound] sendImageMessage failed: ${msg}`);
    return { ok: false, reason: 'SEND_FAIL' };
  }
}