
























@@ -123,13 +123,13 @@ type DispatchTelegramMessageParams = {
123123124124type TelegramReasoningLevel = "off" | "on" | "stream";
125125126-type TelegramAbortFenceState = {
126+type TelegramReplyFenceState = {
127127generation: number;
128128activeDispatches: number;
129129};
130130131131// Newer accepted turns and authorized aborts can arrive ahead of older same-session reply work.
132-const telegramAbortFenceByKey = new Map<string, TelegramAbortFenceState>();
132+const telegramReplyFenceByKey = new Map<string, TelegramReplyFenceState>();
133133134134function normalizeTelegramFenceKey(value: unknown): string | undefined {
135135if (typeof value !== "string") {
@@ -139,7 +139,7 @@ function normalizeTelegramFenceKey(value: unknown): string | undefined {
139139return trimmed.length > 0 ? trimmed : undefined;
140140}
141141142-function resolveTelegramAbortFenceKey(params: {
142+function resolveTelegramReplyFenceKey(params: {
143143ctxPayload: { SessionKey?: string; CommandTargetSessionKey?: string };
144144chatId: number | string;
145145threadSpec: { id?: number | string | null; scope?: string };
@@ -151,41 +151,51 @@ function resolveTelegramAbortFenceKey(params: {
151151);
152152}
153153154-function beginTelegramAbortFence(params: { key: string; supersede: boolean }): number {
155-const existing = telegramAbortFenceByKey.get(params.key);
156-const state: TelegramAbortFenceState = existing ?? {
154+function beginTelegramReplyFence(params: { key: string; supersede: boolean }): number {
155+const existing = telegramReplyFenceByKey.get(params.key);
156+const state: TelegramReplyFenceState = existing ?? {
157157generation: 0,
158158activeDispatches: 0,
159159};
160160if (params.supersede) {
161161state.generation += 1;
162162}
163163state.activeDispatches += 1;
164-telegramAbortFenceByKey.set(params.key, state);
164+telegramReplyFenceByKey.set(params.key, state);
165165return state.generation;
166166}
167167168-function isTelegramAbortFenceSuperseded(params: { key: string; generation: number }): boolean {
169-return (telegramAbortFenceByKey.get(params.key)?.generation ?? 0) !== params.generation;
168+function isTelegramReplyFenceSuperseded(params: { key: string; generation: number }): boolean {
169+return (telegramReplyFenceByKey.get(params.key)?.generation ?? 0) !== params.generation;
170170}
171171172-function endTelegramAbortFence(key: string): void {
173-const state = telegramAbortFenceByKey.get(key);
172+function endTelegramReplyFence(key: string): void {
173+const state = telegramReplyFenceByKey.get(key);
174174if (!state) {
175175return;
176176}
177177state.activeDispatches -= 1;
178178if (state.activeDispatches <= 0) {
179-telegramAbortFenceByKey.delete(key);
179+telegramReplyFenceByKey.delete(key);
180180}
181181}
182182183-export function getTelegramAbortFenceSizeForTests(): number {
184-return telegramAbortFenceByKey.size;
183+function shouldSupersedeTelegramReplyFence(ctxPayload: {
184+Body?: string;
185+RawBody?: string;
186+CommandBody?: string;
187+CommandAuthorized: boolean;
188+}): boolean {
189+const dispatchText = ctxPayload.CommandBody ?? ctxPayload.RawBody ?? ctxPayload.Body ?? "";
190+return !isAbortRequestText(dispatchText) || ctxPayload.CommandAuthorized;
191+}
192+193+export function getTelegramReplyFenceSizeForTests(): number {
194+return telegramReplyFenceByKey.size;
185195}
186196187-export function resetTelegramAbortFenceForTests(): void {
188-telegramAbortFenceByKey.clear();
197+export function resetTelegramReplyFenceForTests(): void {
198+telegramReplyFenceByKey.clear();
189199}
190200191201function resolveTelegramReasoningLevel(params: {
@@ -305,25 +315,25 @@ export const dispatchTelegramMessage = async ({
305315}
306316await statusReactionController.restoreInitial();
307317};
308-const dispatchFenceKey = resolveTelegramAbortFenceKey({
318+const replyFenceKey = resolveTelegramReplyFenceKey({
309319 ctxPayload,
310320 chatId,
311321 threadSpec,
312322});
313-let abortFenceGeneration: number | undefined;
323+let replyFenceGeneration: number | undefined;
314324let dispatchWasSuperseded = false;
315325const isDispatchSuperseded = () =>
316-abortFenceGeneration !== undefined &&
317-isTelegramAbortFenceSuperseded({
318-key: dispatchFenceKey,
319-generation: abortFenceGeneration,
326+replyFenceGeneration !== undefined &&
327+isTelegramReplyFenceSuperseded({
328+key: replyFenceKey,
329+generation: replyFenceGeneration,
320330});
321-const releaseAbortFence = () => {
322-if (abortFenceGeneration === undefined) {
331+const releaseReplyFence = () => {
332+if (replyFenceGeneration === undefined) {
323333return;
324334}
325-endTelegramAbortFence(dispatchFenceKey);
326-abortFenceGeneration = undefined;
335+endTelegramReplyFence(replyFenceKey);
336+replyFenceGeneration = undefined;
327337};
328338const draftMaxChars = Math.min(textLimit, 4096);
329339const tableMode = resolveMarkdownTableMode({
@@ -607,13 +617,10 @@ export const dispatchTelegramMessage = async ({
607617 : undefined;
608618609619const chunkMode = resolveChunkMode(cfg, "telegram", route.accountId);
610-const dispatchText = ctxPayload.CommandBody ?? ctxPayload.RawBody ?? ctxPayload.Body ?? "";
611-const isAbortRequest = isAbortRequestText(dispatchText);
612-const shouldSupersedeAbortFence = isAbortRequest ? ctxPayload.CommandAuthorized : true;
613620614-abortFenceGeneration = beginTelegramAbortFence({
615-key: dispatchFenceKey,
616-supersede: shouldSupersedeAbortFence,
621+replyFenceGeneration = beginTelegramReplyFence({
622+key: replyFenceKey,
623+supersede: shouldSupersedeTelegramReplyFence(ctxPayload),
617624});
618625619626const implicitQuoteReplyTargetId =
@@ -908,7 +915,7 @@ export const dispatchTelegramMessage = async ({
908915909916const flushBufferedFinalAnswer = async () => {
910917const buffered =
911-reasoningStepState.takeBufferedFinalAnswer(abortFenceGeneration);
918+reasoningStepState.takeBufferedFinalAnswer(replyFenceGeneration);
912919if (!buffered) {
913920return;
914921}
@@ -936,7 +943,7 @@ export const dispatchTelegramMessage = async ({
936943reasoningStepState.bufferFinalAnswer({
937944 payload,
938945text: segment.text,
939-bufferedGeneration: abortFenceGeneration,
946+bufferedGeneration: replyFenceGeneration,
940947});
941948continue;
942949}
@@ -1254,7 +1261,7 @@ export const dispatchTelegramMessage = async ({
12541261}
12551262} finally {
12561263dispatchWasSuperseded = isDispatchSuperseded();
1257-releaseAbortFence();
1264+releaseReplyFence();
12581265}
12591266if (dispatchWasSuperseded) {
12601267if (statusReactionController) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。