fix(discord): say when the agent has exited, keep replies in order, settle a stalled response, prune dead tabs

- a reply to a tab whose agent has exited is answered in the thread, with no tick
- inbound messages run on one chain per thread, so a reply with a file cannot be overtaken
- postJson settles when a response stalls after its headers
- the thread map is pruned against the live sessions once after boot
This commit is contained in:
Devvyn committed 2026-10-30 10:33:34 +08:00
1 parent 72a59de96c
commit b68792f988
6 files changed
+108 -4

No files matched your search

+10 -1
View File
@@ -43,7 +43,14 @@ export const postJson: JsonPost = (apiUrl, apiPath, payload, timeoutMs = REQUEST
const url = new URL(apiPath, apiUrl);
const body = JSON.stringify(payload);
const transport = url.protocol === 'https:' ? https : http;
return new Promise((resolve) => {
return new Promise((resolveOnce) => {
// Settle once: a response that stalls after its headers ends in 'close' without 'end'.
let settled = false;
const resolve: typeof resolveOnce = (v) => {
if (settled) return;
settled = true;
resolveOnce(v);
};
const headers: Record<string, string | number> = {
Accept: 'application/json',
'Content-Type': 'application/json',
@@ -64,6 +71,8 @@ export const postJson: JsonPost = (apiUrl, apiPath, payload, timeoutMs = REQUEST
(res) => {
let text = '';
res.on('data', (c) => (text += c));
res.on('error', () => resolve({ status: 0 }));
res.on('close', () => resolve({ status: 0 }));
res.on('end', () => {
let error: string | undefined;
let delivery: { ok: boolean; error?: string } | undefined;