ACP: add persistent Discord channel and Telegram topic bindings (#34873)

* docs: add ACP persistent binding experiment plan

* docs: align ACP persistent binding spec to channel-local config

* docs: scope Telegram ACP bindings to forum topics only

* docs: lock bound /new and /reset behavior to in-place ACP reset

* ACP: add persistent discord/telegram conversation bindings

* ACP: fix persistent binding reuse and discord thread parent context

* docs: document channel-specific persistent ACP bindings

* ACP: split persistent bindings and share conversation id helpers

* ACP: defer configured binding init until preflight passes

* ACP: fix discord thread parent fallback and explicit disable inheritance

* ACP: keep bound /new and /reset in-place

* ACP: honor configured bindings in native command flows

* ACP: avoid configured fallback after runtime bind failure

* docs: refine ACP bindings experiment config examples

* acp: cut over to typed top-level persistent bindings

* ACP bindings: harden reset recovery and native command auth

* Docs: add ACP bound command auth proposal

* Tests: normalize i18n registry zh-CN assertion encoding

* ACP bindings: address review findings for reset and fallback routing

* ACP reset: gate hooks on success and preserve /new arguments

* ACP bindings: fix auth and binding-priority review findings

* Telegram ACP: gate ensure on auth and accepted messages

* ACP bindings: fix session-key precedence and unavailable handling

* ACP reset/native commands: honor fallback targets and abort on bootstrap failure

* Config schema: validate ACP binding channel and Telegram topic IDs

* Discord ACP: apply configured DM bindings to native commands

* ACP reset tails: dispatch through ACP after command handling

* ACP tails/native reset auth: fix target dispatch and restore full auth

* ACP reset detection: fallback to active ACP keys for DM contexts

* Tests: type runTurn mock input in ACP dispatch test

* ACP: dedup binding route bootstrap and reset target resolution

* reply: align ACP reset hooks with bound session key

* docs: replace personal discord ids with placeholders

* fix: add changelog entry for ACP persistent bindings (#34873) (thanks @dutifulbob)

---------

Co-authored-by: Onur <2453968+osolmaz@users.noreply.github.com>
This commit is contained in:
Bob
2026-03-05 09:38:12 +01:00
committed by GitHub
parent 2c8ee593b9
commit 6a705a37f2
50 changed files with 4830 additions and 186 deletions

View File

@@ -27,10 +27,51 @@ describe("commands-acp context", () => {
accountId: "work",
threadId: "thread-42",
conversationId: "thread-42",
parentConversationId: "parent-1",
});
expect(isAcpCommandDiscordChannel(params)).toBe(true);
});
it("resolves discord thread parent from ParentSessionKey when targets point at the thread", () => {
const params = buildCommandTestParams("/acp sessions", baseCfg, {
Provider: "discord",
Surface: "discord",
OriginatingChannel: "discord",
OriginatingTo: "channel:thread-42",
AccountId: "work",
MessageThreadId: "thread-42",
ParentSessionKey: "agent:codex:discord:channel:parent-9",
});
expect(resolveAcpCommandBindingContext(params)).toEqual({
channel: "discord",
accountId: "work",
threadId: "thread-42",
conversationId: "thread-42",
parentConversationId: "parent-9",
});
});
it("resolves discord thread parent from native context when ParentSessionKey is absent", () => {
const params = buildCommandTestParams("/acp sessions", baseCfg, {
Provider: "discord",
Surface: "discord",
OriginatingChannel: "discord",
OriginatingTo: "channel:thread-42",
AccountId: "work",
MessageThreadId: "thread-42",
ThreadParentId: "parent-11",
});
expect(resolveAcpCommandBindingContext(params)).toEqual({
channel: "discord",
accountId: "work",
threadId: "thread-42",
conversationId: "thread-42",
parentConversationId: "parent-11",
});
});
it("falls back to default account and target-derived conversation id", () => {
const params = buildCommandTestParams("/acp status", baseCfg, {
Provider: "slack",
@@ -48,4 +89,23 @@ describe("commands-acp context", () => {
expect(resolveAcpCommandConversationId(params)).toBe("123456789");
expect(isAcpCommandDiscordChannel(params)).toBe(false);
});
it("builds canonical telegram topic conversation ids from originating chat + thread", () => {
const params = buildCommandTestParams("/acp status", baseCfg, {
Provider: "telegram",
Surface: "telegram",
OriginatingChannel: "telegram",
OriginatingTo: "telegram:-1001234567890",
MessageThreadId: "42",
});
expect(resolveAcpCommandBindingContext(params)).toEqual({
channel: "telegram",
accountId: "default",
threadId: "42",
conversationId: "-1001234567890:topic:42",
parentConversationId: "-1001234567890",
});
expect(resolveAcpCommandConversationId(params)).toBe("-1001234567890:topic:42");
});
});

View File

@@ -1,5 +1,10 @@
import {
buildTelegramTopicConversationId,
parseTelegramChatIdFromTarget,
} from "../../../acp/conversation-id.js";
import { DISCORD_THREAD_BINDING_CHANNEL } from "../../../channels/thread-bindings-policy.js";
import { resolveConversationIdFromTargets } from "../../../infra/outbound/conversation-id.js";
import { parseAgentSessionKey } from "../../../routing/session-key.js";
import type { HandleCommandsParams } from "../commands-types.js";
function normalizeString(value: unknown): string {
@@ -33,12 +38,84 @@ export function resolveAcpCommandThreadId(params: HandleCommandsParams): string
}
export function resolveAcpCommandConversationId(params: HandleCommandsParams): string | undefined {
const channel = resolveAcpCommandChannel(params);
if (channel === "telegram") {
const threadId = resolveAcpCommandThreadId(params);
const parentConversationId = resolveAcpCommandParentConversationId(params);
if (threadId && parentConversationId) {
const canonical = buildTelegramTopicConversationId({
chatId: parentConversationId,
topicId: threadId,
});
if (canonical) {
return canonical;
}
}
if (threadId) {
return threadId;
}
}
return resolveConversationIdFromTargets({
threadId: params.ctx.MessageThreadId,
targets: [params.ctx.OriginatingTo, params.command.to, params.ctx.To],
});
}
function parseDiscordParentChannelFromSessionKey(raw: unknown): string | undefined {
const sessionKey = normalizeString(raw);
if (!sessionKey) {
return undefined;
}
const scoped = parseAgentSessionKey(sessionKey)?.rest ?? sessionKey.toLowerCase();
const match = scoped.match(/(?:^|:)channel:([^:]+)$/);
if (!match?.[1]) {
return undefined;
}
return match[1];
}
function parseDiscordParentChannelFromContext(raw: unknown): string | undefined {
const parentId = normalizeString(raw);
if (!parentId) {
return undefined;
}
return parentId;
}
export function resolveAcpCommandParentConversationId(
params: HandleCommandsParams,
): string | undefined {
const channel = resolveAcpCommandChannel(params);
if (channel === "telegram") {
return (
parseTelegramChatIdFromTarget(params.ctx.OriginatingTo) ??
parseTelegramChatIdFromTarget(params.command.to) ??
parseTelegramChatIdFromTarget(params.ctx.To)
);
}
if (channel === DISCORD_THREAD_BINDING_CHANNEL) {
const threadId = resolveAcpCommandThreadId(params);
if (!threadId) {
return undefined;
}
const fromContext = parseDiscordParentChannelFromContext(params.ctx.ThreadParentId);
if (fromContext && fromContext !== threadId) {
return fromContext;
}
const fromParentSession = parseDiscordParentChannelFromSessionKey(params.ctx.ParentSessionKey);
if (fromParentSession && fromParentSession !== threadId) {
return fromParentSession;
}
const fromTargets = resolveConversationIdFromTargets({
targets: [params.ctx.OriginatingTo, params.command.to, params.ctx.To],
});
if (fromTargets && fromTargets !== threadId) {
return fromTargets;
}
}
return undefined;
}
export function isAcpCommandDiscordChannel(params: HandleCommandsParams): boolean {
return resolveAcpCommandChannel(params) === DISCORD_THREAD_BINDING_CHANNEL;
}
@@ -48,11 +125,14 @@ export function resolveAcpCommandBindingContext(params: HandleCommandsParams): {
accountId: string;
threadId?: string;
conversationId?: string;
parentConversationId?: string;
} {
const parentConversationId = resolveAcpCommandParentConversationId(params);
return {
channel: resolveAcpCommandChannel(params),
accountId: resolveAcpCommandAccountId(params),
threadId: resolveAcpCommandThreadId(params),
conversationId: resolveAcpCommandConversationId(params),
...(parentConversationId ? { parentConversationId } : {}),
};
}

View File

@@ -1,5 +1,5 @@
import { callGateway } from "../../../gateway/call.js";
import { getSessionBindingService } from "../../../infra/outbound/session-binding-service.js";
import { resolveEffectiveResetTargetSessionKey } from "../acp-reset-target.js";
import { resolveRequesterSessionKey } from "../commands-subagents/shared.js";
import type { HandleCommandsParams } from "../commands-types.js";
import { resolveAcpCommandBindingContext } from "./context.js";
@@ -35,19 +35,22 @@ async function resolveSessionKeyByToken(token: string): Promise<string | null> {
}
export function resolveBoundAcpThreadSessionKey(params: HandleCommandsParams): string | undefined {
const commandTargetSessionKey =
typeof params.ctx.CommandTargetSessionKey === "string"
? params.ctx.CommandTargetSessionKey.trim()
: "";
const activeSessionKey = commandTargetSessionKey || params.sessionKey.trim();
const bindingContext = resolveAcpCommandBindingContext(params);
if (!bindingContext.channel || !bindingContext.conversationId) {
return undefined;
}
const binding = getSessionBindingService().resolveByConversation({
return resolveEffectiveResetTargetSessionKey({
cfg: params.cfg,
channel: bindingContext.channel,
accountId: bindingContext.accountId,
conversationId: bindingContext.conversationId,
parentConversationId: bindingContext.parentConversationId,
activeSessionKey,
allowNonAcpBindingSessionKey: true,
skipConfiguredFallbackWhenActiveSessionNonAcp: false,
});
if (!binding || binding.targetKind !== "session") {
return undefined;
}
return binding.targetSessionKey.trim() || undefined;
}
export async function resolveAcpTargetSessionKey(params: {