@goodandready/dsh-messenger-gateway 0.4.40 → 0.4.41
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/README.md +0 -0
- package/lib/adapters/telegram.js +0 -0
- package/lib/ask.js +0 -0
- package/lib/documents.js +0 -0
- package/lib/file-manager.js +0 -0
- package/lib/gateway-commands.js +3 -5
- package/lib/gateway-turn.js +11 -3
- package/lib/gateway.js +38 -12
- package/lib/webapp.js +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
File without changes
|
package/lib/adapters/telegram.js
CHANGED
|
File without changes
|
package/lib/ask.js
CHANGED
|
File without changes
|
package/lib/documents.js
CHANGED
|
File without changes
|
package/lib/file-manager.js
CHANGED
|
File without changes
|
package/lib/gateway-commands.js
CHANGED
|
@@ -19,10 +19,6 @@ import { rememberUserFact, listUserMemories, forgetUserMemory } from './memory-b
|
|
|
19
19
|
import { formatSessionCostReport } from './cost-tracker.js'
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
export function releaseChatTurn(chat) {
|
|
23
|
-
chat.busy = Promise.resolve()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
22
|
export async function handleGatewayCommand(gw, key, text, input) {
|
|
27
23
|
const parts = text.split(/\s+/)
|
|
28
24
|
const cmd = parts[0].toLowerCase().split('@')[0]
|
|
@@ -655,8 +651,10 @@ export async function handleGatewayCommand(gw, key, text, input) {
|
|
|
655
651
|
const chat = gw.chats.get(key)
|
|
656
652
|
if (chat?.turnActive || chat?.abort) {
|
|
657
653
|
try { chat.abort?.abort() } catch { /* safe best-effort stop abort */ }
|
|
658
|
-
releaseChatTurn(chat)
|
|
659
654
|
chat.turnActive = false
|
|
655
|
+
// busy deliberately KEEPS the interrupted run's chain (#213): the next
|
|
656
|
+
// turn dispatches as chat.busy.then(...) and therefore starts only
|
|
657
|
+
// after the aborted run has fully drained — no parallel runTurn.
|
|
660
658
|
return reply(t('msg.turn_stopped', {}, locale))
|
|
661
659
|
}
|
|
662
660
|
return reply('Nothing to stop.')
|
package/lib/gateway-turn.js
CHANGED
|
@@ -240,6 +240,10 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
240
240
|
const { reply, typing, startStream, startProgress, react, inboundWasVoice, userId } = input
|
|
241
241
|
const turnStartTime = Date.now()
|
|
242
242
|
chat.turnActive = true
|
|
243
|
+
// Ownership token (#211): the dispatch site stores the AbortController on
|
|
244
|
+
// chat.turnOwner; the finally block below clears chat state only when this
|
|
245
|
+
// turn still owns it, so a stale turn never strips a newer turn's abort.
|
|
246
|
+
const turnOwner = chat.turnOwner
|
|
243
247
|
const sessionId = chat.agent.session.id
|
|
244
248
|
const tg = gw.tg()
|
|
245
249
|
const streaming = tg.streaming === true && typeof startStream === 'function'
|
|
@@ -453,8 +457,12 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
453
457
|
stopTyping()
|
|
454
458
|
if (progress) try { await progress.remove() } catch (err) { gw.logger?.debug?.('progress.remove failed in finally:', err?.message || err) }
|
|
455
459
|
if (typeof react === 'function') react('').catch?.((e) => { gw.logger?.debug?.('react clear error in finally:', e?.message || e) })
|
|
456
|
-
|
|
457
|
-
chat
|
|
458
|
-
|
|
460
|
+
// Ownership-scoped cleanup (#211): if a newer turn already took over the
|
|
461
|
+
// chat state, this stale turn must leave it alone.
|
|
462
|
+
if (chat.turnOwner === turnOwner) {
|
|
463
|
+
chat.turnActive = false
|
|
464
|
+
chat.abort = undefined
|
|
465
|
+
gw.pending.delete(sessionId)
|
|
466
|
+
}
|
|
459
467
|
}
|
|
460
468
|
}
|
package/lib/gateway.js
CHANGED
|
@@ -46,6 +46,8 @@ export class Gateway {
|
|
|
46
46
|
this.config = config
|
|
47
47
|
this.hooks = hooks
|
|
48
48
|
this.chats = new Map()
|
|
49
|
+
// Single-flight chat creation (#215): key -> in-flight createChat promise
|
|
50
|
+
this.chatCreating = new Map()
|
|
49
51
|
this.sessionToChat = new Map()
|
|
50
52
|
this.sessionToThread = new Map()
|
|
51
53
|
this.threadToSession = new Map()
|
|
@@ -174,14 +176,20 @@ export class Gateway {
|
|
|
174
176
|
const turnInput = { ...input, attachments: [], inboundWasVoice: false }
|
|
175
177
|
chat.turnActive = true
|
|
176
178
|
try { chat.abort?.abort?.() } catch { /* safe best-effort abort */ }
|
|
177
|
-
|
|
178
|
-
|
|
179
|
+
const ac = new AbortController()
|
|
180
|
+
// Per-turn ownership token (#211): state cleanup is ownership-scoped, so a
|
|
181
|
+
// stale turn can never clear a newer turn's abort/state.
|
|
182
|
+
chat.turnOwner = ac
|
|
183
|
+
chat.abort = ac
|
|
184
|
+
const run = chat.busy.then(() => this.runTurn(chat, turnInput, ac.signal))
|
|
179
185
|
chat.busy = run.catch((e) => { void e })
|
|
180
186
|
run.catch((err) => {
|
|
181
187
|
const msg = err instanceof Error ? err.message : String(err)
|
|
182
188
|
this.ctx.logger?.warn?.(`dsh-messenger-gateway: cron turn: ${msg}`)
|
|
183
|
-
chat.
|
|
184
|
-
|
|
189
|
+
if (chat.turnOwner === ac) {
|
|
190
|
+
chat.turnActive = false
|
|
191
|
+
chat.abort = undefined
|
|
192
|
+
}
|
|
185
193
|
})
|
|
186
194
|
return run
|
|
187
195
|
}
|
|
@@ -556,8 +564,13 @@ export class Gateway {
|
|
|
556
564
|
// Mark busy BEFORE yielding to the poll loop, otherwise steer/stop never see an active turn.
|
|
557
565
|
chat.turnActive = true
|
|
558
566
|
try { chat.abort?.abort?.() } catch { /* safe best-effort abort */ }
|
|
559
|
-
|
|
560
|
-
|
|
567
|
+
const ac = new AbortController()
|
|
568
|
+
// Per-turn ownership token (#211): state cleanup is ownership-scoped, so
|
|
569
|
+
// a stale turn's finally/error handler can never clear a newer turn's
|
|
570
|
+
// abort/state.
|
|
571
|
+
chat.turnOwner = ac
|
|
572
|
+
chat.abort = ac
|
|
573
|
+
const run = chat.busy.then(() => this.runTurn(chat, turnInput, ac.signal))
|
|
561
574
|
chat.busy = run.catch((e) => { void e })
|
|
562
575
|
// Do NOT await: Telegram poll is sequential; awaiting blocked steer and /stop until the turn finished.
|
|
563
576
|
run.catch((err) => {
|
|
@@ -570,8 +583,10 @@ export class Gateway {
|
|
|
570
583
|
chatId: input.chatId,
|
|
571
584
|
threadId: input.threadId,
|
|
572
585
|
}).catch((e) => this.recordApiFailure('alert.turn', e))
|
|
573
|
-
chat.
|
|
574
|
-
|
|
586
|
+
if (chat.turnOwner === ac) {
|
|
587
|
+
chat.turnActive = false
|
|
588
|
+
chat.abort = undefined
|
|
589
|
+
}
|
|
575
590
|
})
|
|
576
591
|
} catch (err) {
|
|
577
592
|
const msg = err instanceof Error ? err.message : String(err)
|
|
@@ -617,10 +632,21 @@ export class Gateway {
|
|
|
617
632
|
async getOrCreateChat(key, input) {
|
|
618
633
|
let chat = this.chats.get(key)
|
|
619
634
|
if (!chat) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
this.
|
|
635
|
+
// Single-flight (#215): webhook updates and media-group flushes can hit
|
|
636
|
+
// the same key concurrently; without a shared in-flight promise each
|
|
637
|
+
// caller creates its own session and the loser is never disposed.
|
|
638
|
+
let flight = this.chatCreating.get(key)
|
|
639
|
+
if (!flight) {
|
|
640
|
+
flight = (async () => {
|
|
641
|
+
const threadKey = `${input.chatId}:${input.threadId || 0}`
|
|
642
|
+
const existingSessionId = this.threadToSession.get(threadKey)
|
|
643
|
+
const created = await this.createChat(key, input, existingSessionId)
|
|
644
|
+
this.chats.set(key, created)
|
|
645
|
+
return created
|
|
646
|
+
})()
|
|
647
|
+
this.chatCreating.set(key, flight)
|
|
648
|
+
}
|
|
649
|
+
chat = await flight
|
|
624
650
|
}
|
|
625
651
|
chat.lastUsed = Date.now()
|
|
626
652
|
chat.target = { platform: input.platform, chatId: input.chatId, threadId: input.threadId || 0 }
|
package/lib/webapp.js
CHANGED
|
File without changes
|
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.41",
|
|
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",
|