Skip to content

Commit b2846f6

Browse files
fix(clownfish): address review for ghcrawl-156788-autonomous-smoke (1)
Co-authored-by: alex sagit <[email protected]>
1 parent 79e80b3 commit b2846f6

4 files changed

Lines changed: 154 additions & 31 deletions

File tree

‎extensions/feishu/src/monitor.bot-identity.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runti
33
import type { RuntimeEnv } from "../runtime-api.js";
44
import { waitForAbortableDelay } from "./async.js";
55
import { fetchBotIdentityForMonitor, type FeishuMonitorBotIdentity } from "./monitor.startup.js";
6-
import { botNames, botOpenIds } from "./monitor.state.js";
6+
import { setFeishuBotIdentityState } from "./monitor.state.js";
77
import type { ResolvedFeishuAccount } from "./types.js";
88

99
// Delays must be >= PROBE_ERROR_TTL_MS (60s) so each retry makes a real network request
@@ -17,12 +17,7 @@ export function applyBotIdentityState(
1717
const botOpenId = normalizeOptionalString(identity.botOpenId);
1818
const botName = normalizeOptionalString(identity.botName);
1919

20-
botOpenIds.set(accountId, botOpenId ?? "");
21-
if (botName) {
22-
botNames.set(accountId, botName);
23-
} else {
24-
botNames.delete(accountId);
25-
}
20+
setFeishuBotIdentityState(accountId, { botOpenId: botOpenId ?? "", botName });
2621

2722
return { botOpenId, botName };
2823
}

‎extensions/feishu/src/monitor.cleanup.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
botOpenIds,
77
FEISHU_HTTP_SERVER_CLOSE_TIMEOUT_MS,
88
httpServers,
9+
setFeishuBotIdentityState,
910
stopFeishuMonitorState,
1011
wsClients,
1112
} from "./monitor.state.js";
@@ -442,6 +443,53 @@ describe("feishu websocket cleanup", () => {
442443
expect(botNames.has("alpha")).toBe(false);
443444
});
444445

446+
it("preserves replacement HTTP state after delayed targeted cleanup", async () => {
447+
const oldServer = createHttpServerMock();
448+
const replacementServer = createHttpServerMock();
449+
450+
httpServers.set("alpha", oldServer.server);
451+
setFeishuBotIdentityState("alpha", { botOpenId: "ou_old", botName: "Old" });
452+
453+
const stopPromise = stopFeishuMonitorState("alpha");
454+
await Promise.resolve();
455+
456+
setFeishuBotIdentityState("alpha", { botOpenId: "ou_new", botName: "New" });
457+
httpServers.set("alpha", replacementServer.server);
458+
459+
oldServer.finishClose();
460+
await stopPromise;
461+
462+
expect(httpServers.get("alpha")).toBe(replacementServer.server);
463+
expect(botOpenIds.get("alpha")).toBe("ou_new");
464+
expect(botNames.get("alpha")).toBe("New");
465+
466+
const cleanupPromise = stopFeishuMonitorState("alpha");
467+
await Promise.resolve();
468+
replacementServer.finishClose();
469+
await cleanupPromise;
470+
});
471+
472+
it("preserves replacement identity written before the replacement HTTP server is tracked", async () => {
473+
const oldServer = createHttpServerMock();
474+
475+
httpServers.set("alpha", oldServer.server);
476+
setFeishuBotIdentityState("alpha", { botOpenId: "ou_old", botName: "Old" });
477+
478+
const stopPromise = stopFeishuMonitorState("alpha");
479+
await Promise.resolve();
480+
481+
setFeishuBotIdentityState("alpha", { botOpenId: "ou_new", botName: "New" });
482+
483+
oldServer.finishClose();
484+
await stopPromise;
485+
486+
expect(httpServers.has("alpha")).toBe(false);
487+
expect(botOpenIds.get("alpha")).toBe("ou_new");
488+
expect(botNames.get("alpha")).toBe("New");
489+
490+
await stopFeishuMonitorState("alpha");
491+
});
492+
445493
it("forces targeted HTTP server cleanup after the close timeout", async () => {
446494
vi.useFakeTimers();
447495
const { server, close, closeAllConnections } = createHttpServerMock();
@@ -468,4 +516,30 @@ describe("feishu websocket cleanup", () => {
468516
expect(botOpenIds.has("alpha")).toBe(false);
469517
expect(botNames.has("alpha")).toBe(false);
470518
});
519+
520+
it("preserves replacement HTTP state after delayed global cleanup", async () => {
521+
const oldServer = createHttpServerMock();
522+
const replacementServer = createHttpServerMock();
523+
524+
httpServers.set("alpha", oldServer.server);
525+
setFeishuBotIdentityState("alpha", { botOpenId: "ou_old", botName: "Old" });
526+
527+
const stopPromise = stopFeishuMonitorState();
528+
await Promise.resolve();
529+
530+
setFeishuBotIdentityState("alpha", { botOpenId: "ou_new", botName: "New" });
531+
httpServers.set("alpha", replacementServer.server);
532+
533+
oldServer.finishClose();
534+
await stopPromise;
535+
536+
expect(httpServers.get("alpha")).toBe(replacementServer.server);
537+
expect(botOpenIds.get("alpha")).toBe("ou_new");
538+
expect(botNames.get("alpha")).toBe("New");
539+
540+
const cleanupPromise = stopFeishuMonitorState("alpha");
541+
await Promise.resolve();
542+
replacementServer.finishClose();
543+
await cleanupPromise;
544+
});
471545
});

‎extensions/feishu/src/monitor.state.ts‎

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export const wsClients = new Map<string, Lark.WSClient>();
1313
export const httpServers = new Map<string, http.Server>();
1414
export const botOpenIds = new Map<string, string>();
1515
export const botNames = new Map<string, string>();
16+
// HTTP close is awaited, so a replacement monitor can write identity before
17+
// registering its replacement server. Revisions keep stale close cleanup from
18+
// erasing that newer identity.
19+
const botIdentityRevisions = new Map<string, number>();
1620

1721
export const FEISHU_WEBHOOK_MAX_BODY_BYTES = 64 * 1024;
1822
export const FEISHU_WEBHOOK_BODY_TIMEOUT_MS = 5_000;
@@ -30,6 +34,10 @@ type WebhookAnomalyDefaults = {
3034
logEvery: number;
3135
};
3236

37+
type BotIdentitySnapshot = {
38+
revision: number;
39+
};
40+
3341
const FEISHU_WEBHOOK_RATE_LIMIT_FALLBACK_DEFAULTS: WebhookRateLimitDefaults = {
3442
windowMs: 60_000,
3543
maxRequests: 120,
@@ -117,6 +125,57 @@ function closeWsClient(client: Lark.WSClient | undefined): void {
117125
}
118126
}
119127

128+
function readBotIdentityRevision(accountId: string): number {
129+
return botIdentityRevisions.get(accountId) ?? 0;
130+
}
131+
132+
function bumpBotIdentityRevision(accountId: string): void {
133+
botIdentityRevisions.set(accountId, readBotIdentityRevision(accountId) + 1);
134+
}
135+
136+
function captureBotIdentitySnapshot(accountId: string): BotIdentitySnapshot {
137+
return { revision: readBotIdentityRevision(accountId) };
138+
}
139+
140+
function captureBotIdentitySnapshots(): Array<[accountId: string, snapshot: BotIdentitySnapshot]> {
141+
const accountIds = new Set([...botOpenIds.keys(), ...botNames.keys()]);
142+
return Array.from(accountIds, (accountId): [string, BotIdentitySnapshot] => [
143+
accountId,
144+
captureBotIdentitySnapshot(accountId),
145+
]);
146+
}
147+
148+
function clearFeishuBotIdentityStateIfUnchanged(
149+
accountId: string,
150+
snapshot: BotIdentitySnapshot,
151+
): void {
152+
if (readBotIdentityRevision(accountId) !== snapshot.revision) {
153+
return;
154+
}
155+
botOpenIds.delete(accountId);
156+
botNames.delete(accountId);
157+
bumpBotIdentityRevision(accountId);
158+
}
159+
160+
export function setFeishuBotIdentityState(
161+
accountId: string,
162+
identity: { botOpenId: string; botName: string | undefined },
163+
): void {
164+
botOpenIds.set(accountId, identity.botOpenId);
165+
if (identity.botName) {
166+
botNames.set(accountId, identity.botName);
167+
} else {
168+
botNames.delete(accountId);
169+
}
170+
bumpBotIdentityRevision(accountId);
171+
}
172+
173+
export function clearFeishuBotIdentityState(accountId: string): void {
174+
botOpenIds.delete(accountId);
175+
botNames.delete(accountId);
176+
bumpBotIdentityRevision(accountId);
177+
}
178+
120179
function isServerNotRunningError(error: Error): boolean {
121180
return (error as NodeJS.ErrnoException).code === "ERR_SERVER_NOT_RUNNING";
122181
}
@@ -155,23 +214,26 @@ export async function closeFeishuHttpServer(server: http.Server): Promise<void>
155214
});
156215
}
157216

158-
async function closeTrackedHttpServer(accountId: string, server: http.Server): Promise<void> {
217+
export async function closeTrackedFeishuHttpServer(
218+
accountId: string,
219+
server: http.Server,
220+
): Promise<void> {
221+
const identitySnapshot = captureBotIdentitySnapshot(accountId);
159222
try {
160223
await closeFeishuHttpServer(server);
161224
} finally {
162225
if (httpServers.get(accountId) === server) {
163226
httpServers.delete(accountId);
227+
clearFeishuBotIdentityStateIfUnchanged(accountId, identitySnapshot);
164228
}
165-
botOpenIds.delete(accountId);
166-
botNames.delete(accountId);
167229
}
168230
}
169231

170232
async function closeTrackedHttpServers(
171233
entries: Array<[accountId: string, server: http.Server]>,
172234
): Promise<void> {
173235
const results = await Promise.allSettled(
174-
entries.map(([accountId, server]) => closeTrackedHttpServer(accountId, server)),
236+
entries.map(([accountId, server]) => closeTrackedFeishuHttpServer(accountId, server)),
175237
);
176238
const rejected = results.find(
177239
(result): result is PromiseRejectedResult => result.status === "rejected",
@@ -215,23 +277,25 @@ export async function stopFeishuMonitorState(accountId?: string): Promise<void>
215277
wsClients.delete(accountId);
216278
const server = httpServers.get(accountId);
217279
if (server) {
218-
await closeTrackedHttpServer(accountId, server);
280+
await closeTrackedFeishuHttpServer(accountId, server);
219281
return;
220282
}
221-
botOpenIds.delete(accountId);
222-
botNames.delete(accountId);
283+
clearFeishuBotIdentityState(accountId);
223284
return;
224285
}
225286

226287
for (const client of wsClients.values()) {
227288
closeWsClient(client);
228289
}
229290
wsClients.clear();
291+
const identitySnapshots = captureBotIdentitySnapshots();
230292
try {
231293
await closeTrackedHttpServers([...httpServers.entries()]);
232294
} finally {
233-
httpServers.clear();
234-
botOpenIds.clear();
235-
botNames.clear();
295+
for (const [identityAccountId, snapshot] of identitySnapshots) {
296+
if (!httpServers.has(identityAccountId)) {
297+
clearFeishuBotIdentityStateIfUnchanged(identityAccountId, snapshot);
298+
}
299+
}
236300
}
237301
}

‎extensions/feishu/src/monitor.transport.ts‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import {
1313
type RuntimeEnv,
1414
} from "./monitor-transport-runtime-api.js";
1515
import {
16-
botNames,
17-
botOpenIds,
18-
closeFeishuHttpServer,
16+
clearFeishuBotIdentityState,
17+
closeTrackedFeishuHttpServer,
1918
FEISHU_WEBHOOK_BODY_TIMEOUT_MS,
2019
FEISHU_WEBHOOK_MAX_BODY_BYTES,
2120
feishuWebhookRateLimiter,
@@ -174,8 +173,7 @@ function cleanupFeishuWsClient(params: {
174173
}
175174
wsClients.delete(accountId);
176175
if (clearIdentity) {
177-
botOpenIds.delete(accountId);
178-
botNames.delete(accountId);
176+
clearFeishuBotIdentityState(accountId);
179177
}
180178
}
181179

@@ -424,15 +422,7 @@ export async function monitorWebhook({
424422
return;
425423
}
426424
cleanupStarted = true;
427-
try {
428-
await closeFeishuHttpServer(server);
429-
} finally {
430-
if (httpServers.get(accountId) === server) {
431-
httpServers.delete(accountId);
432-
}
433-
botOpenIds.delete(accountId);
434-
botNames.delete(accountId);
435-
}
425+
await closeTrackedFeishuHttpServer(accountId, server);
436426
};
437427

438428
const handleAbort = () => {

0 commit comments

Comments
 (0)