@goodandready/dsh-messenger-gateway 0.4.41 → 0.4.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/gateway-commands.js +14 -0
- package/lib/gateway-turn.js +22 -3
- package/lib/gateway.js +0 -0
- package/lib/stream.js +15 -1
- package/package.json +1 -1
package/lib/gateway-commands.js
CHANGED
|
@@ -244,6 +244,20 @@ export async function handleGatewayCommand(gw, key, text, input) {
|
|
|
244
244
|
const sessions = gw.ctx.get?.('sessions') || gw.ctx.sessions
|
|
245
245
|
try { await sessions?.flush(newChat.agent.session) } catch (err) { gw.logger?.debug?.('fork session flush error:', err?.message || err) }
|
|
246
246
|
}
|
|
247
|
+
// Full teardown of the old chat before replacement (#214): the old
|
|
248
|
+
// agent must stop receiving events and the id mappings must not route
|
|
249
|
+
// ask/approvals from the old session into the new chat.
|
|
250
|
+
try {
|
|
251
|
+
await chat.dispose()
|
|
252
|
+
} catch (err) { gw.logger?.debug?.('fork: old chat dispose error:', err?.message || err) }
|
|
253
|
+
const oldId = String(oldSession.id)
|
|
254
|
+
gw.sessionToChat.delete(oldId)
|
|
255
|
+
const threadInfo = gw.sessionToThread.get(oldId)
|
|
256
|
+
if (threadInfo) {
|
|
257
|
+
gw.threadToSession.delete(`${threadInfo.chatId}:${threadInfo.threadId}`)
|
|
258
|
+
gw.sessionToThread.delete(oldId)
|
|
259
|
+
}
|
|
260
|
+
gw.pending.delete(oldId)
|
|
247
261
|
gw.chats.set(key, newChat)
|
|
248
262
|
return reply(`🔀 Forked session!\nOld session: ${oldSession.id}\nNew session: ${newChat.agent.session.id}\nContext preserved (${oldMessages.length} messages).`)
|
|
249
263
|
} catch (err) {
|
package/lib/gateway-turn.js
CHANGED
|
@@ -23,12 +23,24 @@ const PLUGIN = 'dsh-messenger-gateway'
|
|
|
23
23
|
export function whenIdleWithTimeout(agent, timeoutMs, signal) {
|
|
24
24
|
const idle = agent.whenIdle()
|
|
25
25
|
if (!timeoutMs || timeoutMs <= 0) return idle
|
|
26
|
+
// Full cleanup (#220): on ANY resolution path (idle win, timeout, abort) the
|
|
27
|
+
// timer is cleared and the abort listener removed — no leaks accumulate
|
|
28
|
+
// across long-lived chats.
|
|
29
|
+
let timer = null
|
|
30
|
+
let onAbort = null
|
|
31
|
+
const cleanup = () => {
|
|
32
|
+
if (timer) { clearTimeout(timer); timer = null }
|
|
33
|
+
if (signal && onAbort) { signal.removeEventListener('abort', onAbort); onAbort = null }
|
|
34
|
+
}
|
|
26
35
|
return Promise.race([
|
|
27
|
-
idle,
|
|
36
|
+
idle.finally(cleanup),
|
|
28
37
|
new Promise((_, reject) => {
|
|
29
|
-
|
|
38
|
+
timer = setTimeout(() => { cleanup(); reject(new Error('turn timeout (' + timeoutMs + 'ms)')) }, timeoutMs)
|
|
30
39
|
timer.unref?.()
|
|
31
|
-
|
|
40
|
+
if (signal) {
|
|
41
|
+
onAbort = () => { cleanup(); reject(new DOMException('Aborted', 'AbortError')) }
|
|
42
|
+
signal.addEventListener('abort', onAbort, { once: true })
|
|
43
|
+
}
|
|
32
44
|
}),
|
|
33
45
|
])
|
|
34
46
|
}
|
|
@@ -311,6 +323,8 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
311
323
|
const turnTimeoutMs = Number(gw.config.agent?.turnTimeoutMs) || 600_000
|
|
312
324
|
await whenIdleWithTimeout(chat.agent, turnTimeoutMs, signal)
|
|
313
325
|
if (signal.aborted) {
|
|
326
|
+
// Stale edits must not overwrite the stopped message (#216)
|
|
327
|
+
scheduler?.cancel()
|
|
314
328
|
if (progress) try { await progress.remove() } catch (err) { gw.logger?.debug?.('progress.remove failed on abort:', err?.message || err) }
|
|
315
329
|
if (typeof react === 'function') react('').catch?.((e) => { gw.logger?.debug?.('react clear error:', e?.message || e) })
|
|
316
330
|
const stoppedMsg = t('msg.turn_stopped', {}, gw.resolveLocale(input))
|
|
@@ -437,6 +451,8 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
437
451
|
})
|
|
438
452
|
}
|
|
439
453
|
} catch (err) {
|
|
454
|
+
// Stale edits must not overwrite the error message (#216)
|
|
455
|
+
scheduler?.cancel()
|
|
440
456
|
if (!signal?.aborted) {
|
|
441
457
|
gw.sendAlert('error', {
|
|
442
458
|
code: err?.code || 'EXCEPTION',
|
|
@@ -455,6 +471,9 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
455
471
|
}
|
|
456
472
|
} finally {
|
|
457
473
|
stopTyping()
|
|
474
|
+
// Drop pending edits: after finalize/abort nothing may overwrite the
|
|
475
|
+
// delivered message (#216)
|
|
476
|
+
scheduler?.cancel()
|
|
458
477
|
if (progress) try { await progress.remove() } catch (err) { gw.logger?.debug?.('progress.remove failed in finally:', err?.message || err) }
|
|
459
478
|
if (typeof react === 'function') react('').catch?.((e) => { gw.logger?.debug?.('react clear error in finally:', e?.message || e) })
|
|
460
479
|
// Ownership-scoped cleanup (#211): if a newer turn already took over the
|
package/lib/gateway.js
CHANGED
|
File without changes
|
package/lib/stream.js
CHANGED
|
@@ -171,9 +171,11 @@ export function createEditScheduler(editFn, intervalMs = 1200, firstChunkDelayMs
|
|
|
171
171
|
let lastSent = ''
|
|
172
172
|
let inFlight = Promise.resolve()
|
|
173
173
|
let retryDelayMs = 0
|
|
174
|
+
let cancelled = false
|
|
174
175
|
|
|
175
176
|
const flush = () => {
|
|
176
177
|
timer = null
|
|
178
|
+
if (cancelled) return
|
|
177
179
|
if (pending === null || pending === lastSent) return
|
|
178
180
|
const text = pending
|
|
179
181
|
inFlight = inFlight.then(async () => {
|
|
@@ -183,12 +185,13 @@ export function createEditScheduler(editFn, intervalMs = 1200, firstChunkDelayMs
|
|
|
183
185
|
retryDelayMs = 0
|
|
184
186
|
await new Promise((r) => setTimeout(r, delay))
|
|
185
187
|
}
|
|
188
|
+
if (cancelled) return
|
|
186
189
|
await editFn(text)
|
|
187
190
|
lastSent = text
|
|
188
191
|
} catch (err) {
|
|
189
192
|
logger?.debug?.('stream chunk edit failed:', err?.message || err)
|
|
190
193
|
const retry = parseRetryAfter(err)
|
|
191
|
-
if (retry > 0) {
|
|
194
|
+
if (retry > 0 && !cancelled) {
|
|
192
195
|
retryDelayMs = retry
|
|
193
196
|
// Re-schedule flush with retry delay
|
|
194
197
|
if (!timer) {
|
|
@@ -202,6 +205,7 @@ export function createEditScheduler(editFn, intervalMs = 1200, firstChunkDelayMs
|
|
|
202
205
|
|
|
203
206
|
return {
|
|
204
207
|
push(text) {
|
|
208
|
+
if (cancelled) return
|
|
205
209
|
pending = text
|
|
206
210
|
if (timer) return
|
|
207
211
|
// Fast first chunk: when lastSent is empty, dispatch quickly so user sees immediate response.
|
|
@@ -210,16 +214,26 @@ export function createEditScheduler(editFn, intervalMs = 1200, firstChunkDelayMs
|
|
|
210
214
|
timer = setTimeout(flush, delay)
|
|
211
215
|
timer.unref?.()
|
|
212
216
|
},
|
|
217
|
+
// cancel() drops every pending edit and stops the timer: after finalize,
|
|
218
|
+
// abort or error the message must not be overwritten by stale text (#216).
|
|
219
|
+
cancel() {
|
|
220
|
+
cancelled = true
|
|
221
|
+
pending = null
|
|
222
|
+
if (timer) { clearTimeout(timer); timer = null }
|
|
223
|
+
},
|
|
213
224
|
async flush() {
|
|
214
225
|
if (timer) { clearTimeout(timer); timer = null }
|
|
226
|
+
if (cancelled) return
|
|
215
227
|
flush()
|
|
216
228
|
await inFlight
|
|
229
|
+
if (cancelled) return
|
|
217
230
|
// If there's still a pending difference (e.g. rate limit delay happened), wait and retry
|
|
218
231
|
if (pending !== null && pending !== lastSent) {
|
|
219
232
|
if (retryDelayMs > 0) {
|
|
220
233
|
await new Promise((r) => setTimeout(r, retryDelayMs + 50))
|
|
221
234
|
retryDelayMs = 0
|
|
222
235
|
}
|
|
236
|
+
if (cancelled) return
|
|
223
237
|
try {
|
|
224
238
|
await editFn(pending)
|
|
225
239
|
lastSent = pending
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-messenger-gateway",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.42",
|
|
4
4
|
"description": "Telegram messenger bridge for DeepSeek Harness: sessions, steer, homes, inline asks, notify bridge, and optional TTS voice notes.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|