polygram 0.31.0 → 0.32.0

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/polygram.js CHANGED
@@ -119,6 +119,7 @@ const { formatContextReply, maybeContextFullHint } = require('./lib/context-form
119
119
  const { createAbortGrace } = require('./lib/abort-grace');
120
120
  const agentLoader = require('./lib/agents/loader');
121
121
  const { createSender } = require('./lib/telegram/api');
122
+ const { createDeliveryBarrier } = require('./lib/telegram/delivery-barrier');
122
123
  const { createAsyncLock } = require('@shumkov/orchestra');
123
124
  const { sweepInbox } = require('./lib/db/inbox');
124
125
  const { parseBotArg, parseDbArg, filterConfigToBot, activeBotConfig } = require('./lib/config-scope');
@@ -177,6 +178,17 @@ const { createAuthDisabledGate } = require('./lib/ops/auth-disabled-gate');
177
178
  const { createHeartbeat } = require('./lib/ops/heartbeat');
178
179
  const { createCgroupOomObserver } = require('./lib/ops/cgroup-oom-observer');
179
180
  const { persistShutdownDisposition } = require('./lib/ops/shutdown-disposition');
181
+ const {
182
+ prepareCleanRetirement,
183
+ buildResumeIntents,
184
+ } = require('./lib/ops/clean-shutdown');
185
+ const {
186
+ CLEAN_RESUME_POLICY_VERSION,
187
+ createCleanResumeTurnContext,
188
+ executeCleanResumeClaim,
189
+ startCleanRestartRecovery,
190
+ validateStrictResumeSpawn,
191
+ } = require('./lib/ops/clean-resume');
180
192
  const { resolveReplayWindowMs } = require('./lib/db/replay-window');
181
193
  const { pruneEvents, resolveRetentionPolicy, validatePolicy } = require('./lib/db/events-retention');
182
194
  const { sweepSecrets, resolveSecretSweepConfig } = require('./lib/db/secret-sweep');
@@ -226,6 +238,7 @@ let emojiToSticker = {}; // emoji → file_id
226
238
  let config;
227
239
  let db;
228
240
  let tg; // unified sender, created after db opens
241
+ let deliveryBarrier = null;
229
242
  let pairings; // pairings store, created after db opens
230
243
  let approvals; // approvals store, created after db opens
231
244
  // approvalWaiters Map moved to lib/handlers/approvals.js (commit 29).
@@ -753,7 +766,10 @@ async function resolveSessionRuntimeView({
753
766
 
754
767
  async function buildSpawnContext(
755
768
  sessionKey,
756
- { mutateSessionOnDrift = true } = {},
769
+ {
770
+ mutateSessionOnDrift = true,
771
+ strictResume = null,
772
+ } = {},
757
773
  ) {
758
774
  const chatId = getChatIdFromKey(sessionKey);
759
775
  const chatConfig = config.chats[chatId];
@@ -765,6 +781,12 @@ async function buildSpawnContext(
765
781
  threadId: threadId || null,
766
782
  });
767
783
 
784
+ if (strictResume && promptBackend === 'codex') {
785
+ validateStrictResumeSpawn({
786
+ strictResume,
787
+ promptBackend,
788
+ });
789
+ }
768
790
  if (promptBackend === 'codex') {
769
791
  if (!codexRuntimeController) {
770
792
  const error = new Error('Codex runtime controller is not initialized');
@@ -809,7 +831,22 @@ async function buildSpawnContext(
809
831
  promptBackend,
810
832
  );
811
833
  let existingSessionId;
812
- if (isColdSpawn) {
834
+ if (strictResume) {
835
+ const topicConfig = getTopicConfig(chatConfig, threadId || null);
836
+ const resolved = {
837
+ agent: topicConfig.agent || chatConfig.agent || null,
838
+ cwd: topicConfig.cwd || chatConfig.cwd || null,
839
+ backend: pickBackend({ config, chatId, threadId: threadId || null, pmDefault: 'sdk' }),
840
+ };
841
+ const runtime = db.getProviderSession(sessionKey, 'claude:channels');
842
+ existingSessionId = validateStrictResumeSpawn({
843
+ strictResume,
844
+ promptBackend,
845
+ liveProcess,
846
+ runtime,
847
+ resolved,
848
+ });
849
+ } else if (isColdSpawn) {
813
850
  const topicConfig = getTopicConfig(chatConfig, threadId || null);
814
851
  const resolved = {
815
852
  agent: topicConfig.agent || chatConfig.agent || null,
@@ -843,6 +880,11 @@ async function buildSpawnContext(
843
880
  threadId: threadId || null,
844
881
  label: getSessionLabel(chatConfig, threadId),
845
882
  existingSessionId,
883
+ ...(strictResume ? {
884
+ resumePolicy: 'require-existing-session',
885
+ expectedSessionId: strictResume.expectedSessionId,
886
+ noWaitForCapacity: true,
887
+ } : {}),
846
888
  // File-send outbound cap inputs: localApi (backend ceiling) + the resolved
847
889
  // per-file override (topic → chat → bot → default) from the SAME resolver
848
890
  // the inbound filter and the send() choke point use, so CliProcess's
@@ -866,8 +908,13 @@ async function getOrSpawnForChat(sessionKey) {
866
908
  return pm.getOrSpawn(sessionKey, ctx);
867
909
  }
868
910
 
869
- async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched } = {}) {
870
- const entry = await getOrSpawnForChat(sessionKey);
911
+ async function sendToProcess(
912
+ sessionKey,
913
+ prompt,
914
+ context = {},
915
+ { onDispatched, expectedProcess = null } = {},
916
+ ) {
917
+ const entry = expectedProcess || await getOrSpawnForChat(sessionKey);
871
918
  if (!entry) throw new Error('No process for chat');
872
919
  const chatId = getChatIdFromKey(sessionKey);
873
920
  const chatConfig = config.chats[chatId];
@@ -900,6 +947,7 @@ async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched }
900
947
  maxTurnMs,
901
948
  maxTurnHardMs,
902
949
  context,
950
+ ...(expectedProcess ? { expectedProcess } : {}),
903
951
  });
904
952
  if (typeof onDispatched === 'function') onDispatched();
905
953
  return await turnP;
@@ -934,7 +982,13 @@ async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched }
934
982
  // starts, which is the correct UX (and what the user already expects).
935
983
  const release = await stdinLock.acquire(sessionKey);
936
984
  try {
937
- const turnP = pm.send(sessionKey, prompt, { timeoutMs, maxTurnMs, maxTurnHardMs, context });
985
+ const turnP = pm.send(sessionKey, prompt, {
986
+ timeoutMs,
987
+ maxTurnMs,
988
+ maxTurnHardMs,
989
+ context,
990
+ ...(expectedProcess ? { expectedProcess } : {}),
991
+ });
938
992
  // Phase 3 §4: pm.send synchronously kicks off the turn — the
939
993
  // process is now inFlight. Signal the committed-intent latch so
940
994
  // it can release; a concurrent handler will then correctly see
@@ -975,6 +1029,8 @@ let attemptAutoResume = null;
975
1029
  let errorReplyText = null;
976
1030
  let queueWarnThreshold = null;
977
1031
  let inFlightHandlers = null;
1032
+ let awaitHandlerSettlement = null;
1033
+ let trackHandlerTask = null;
978
1034
  let recoverCodexRequest = null;
979
1035
 
980
1036
  // AUTH_DISABLED (docs/AUTH_DISABLED_HANDLING_SPEC.md): dedupe/re-arm gate for
@@ -1373,7 +1429,12 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1373
1429
  await tg(bot, 'sendMessage', {
1374
1430
  chat_id: chatId, text: `Attachment(s) skipped: ${summary.slice(0, 300)}`,
1375
1431
  ...replyOpts(threadId),
1376
- }, { source: 'attachment-skipped', botName: BOT_NAME });
1432
+ }, {
1433
+ source: 'attachment-skipped',
1434
+ botName: BOT_NAME,
1435
+ sessionKey,
1436
+ sourceMsgId: msg.message_id,
1437
+ });
1377
1438
  } catch (err) {
1378
1439
  // Surface the failure: claude is about to reply as if the photo
1379
1440
  // was processed (because filterAttachments dropped it before
@@ -1416,6 +1477,8 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1416
1477
  const outMetaBase = {
1417
1478
  source: 'bot-reply-stream',
1418
1479
  botName: BOT_NAME,
1480
+ sessionKey,
1481
+ sourceMsgId: msg.message_id,
1419
1482
  model: chatConfig.model,
1420
1483
  effort: chatConfig.effort,
1421
1484
  ...(linkPreview === false ? { linkPreview: false } : {}),
@@ -1503,6 +1566,8 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1503
1566
  threadId,
1504
1567
  replyToMessageId: msg.message_id,
1505
1568
  botName: BOT_NAME,
1569
+ sessionKey,
1570
+ sourceMsgId: msg.message_id,
1506
1571
  logEvent: (kind, detail) => logEvent(kind, {
1507
1572
  chat_id: chatId, thread_id: threadId, bot: BOT_NAME, ...detail,
1508
1573
  }),
@@ -1609,6 +1674,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1609
1674
  phase: payload.phase,
1610
1675
  mediaContext,
1611
1676
  hadReplyAnchor: payload.hadReplyAnchor,
1677
+ meta: outMetaBase,
1612
1678
  });
1613
1679
  }
1614
1680
  const text = payload;
@@ -1623,7 +1689,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1623
1689
  chat_id: chatId,
1624
1690
  message_id: messageId,
1625
1691
  text: sanitizeLiveText(text),
1626
- }, { source: 'bot-reply-stream-edit', botName: BOT_NAME });
1692
+ }, { ...outMetaBase, source: 'bot-reply-stream-edit' });
1627
1693
  } catch (err) {
1628
1694
  // Stream-edit failures would otherwise be invisible — edits
1629
1695
  // don't insert a messages row by default (tg() does, but we
@@ -1648,7 +1714,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1648
1714
  try {
1649
1715
  await tg(bot, 'deleteMessage', {
1650
1716
  chat_id: chatId, message_id: messageId,
1651
- }, { source: 'bot-reply-stream-discard', botName: BOT_NAME });
1717
+ }, { ...outMetaBase, source: 'bot-reply-stream-discard' });
1652
1718
  } catch (err) {
1653
1719
  console.error(`[${label}] discard preview failed: ${err.message}`);
1654
1720
  throw err;
@@ -2578,7 +2644,12 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
2578
2644
  chat_id: chatId,
2579
2645
  message_id: msg.message_id,
2580
2646
  reaction: [{ type: 'emoji', emoji }],
2581
- }, { ...outMeta, source: 'inline-reaction', reaction: emoji });
2647
+ }, {
2648
+ ...outMeta,
2649
+ deliveryClass: 'reply-bearing',
2650
+ source: 'inline-reaction',
2651
+ reaction: emoji,
2652
+ });
2582
2653
  } catch (err) {
2583
2654
  console.error(`[${label}] inline setMessageReaction(${emoji}) failed: ${err.message}`);
2584
2655
  }
@@ -2705,7 +2776,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
2705
2776
  chat_id: chatId,
2706
2777
  message_id: msg.message_id,
2707
2778
  reaction: [{ type: 'emoji', emoji: parsed.reaction }],
2708
- }, outMeta).catch((err) => {
2779
+ }, { ...outMeta, deliveryClass: 'reply-bearing' }).catch((err) => {
2709
2780
  telegramDeliveryComplete = false;
2710
2781
  console.error(`[${label}] setMessageReaction failed: ${err.message}`);
2711
2782
  });
@@ -3447,7 +3518,8 @@ async function main() {
3447
3518
  );
3448
3519
  }
3449
3520
  }
3450
- tg = createSender(db, console, config);
3521
+ deliveryBarrier = createDeliveryBarrier();
3522
+ tg = createSender(db, console, config, deliveryBarrier);
3451
3523
  richMediaFileIdCache = createMediaFileIdCache();
3452
3524
  // ONE latch decision for both rich paths. They set the same flag, so a
3453
3525
  // counter owned by either would let that path latch on its first bare
@@ -3712,6 +3784,7 @@ async function main() {
3712
3784
 
3713
3785
  const sdkCallbacks = createSdkCallbacks({
3714
3786
  db, dbWrite, config, bot, botName: BOT_NAME, tg, logEvent, sessionFeedback,
3787
+ deliveryBarrier,
3715
3788
  classifyToolName, announce, shouldAnnounce, contextHintShown,
3716
3789
  extractAssistantText, getChatIdFromKey, getThreadIdFromKey,
3717
3790
  // F#23: enable parse/sanitize/sticker/react on the autonomous-wakeup path.
@@ -4131,6 +4204,8 @@ async function main() {
4131
4204
  errorReplyText,
4132
4205
  queueWarnThreshold,
4133
4206
  inFlightHandlers,
4207
+ awaitSettlement: awaitHandlerSettlement,
4208
+ trackTask: trackHandlerTask,
4134
4209
  } = createDispatcher({
4135
4210
  config, db, dbWrite, tg, botName: BOT_NAME, logEvent,
4136
4211
  handleMessage, sendToProcess,
@@ -4342,18 +4417,91 @@ async function main() {
4342
4417
  const drainElapsed = Date.now() - drainStart;
4343
4418
  const remaining = countInFlight(inFlightHandlers);
4344
4419
 
4345
- // 3. A handled signal is normally deliberate, but a Linux supervisor can
4346
- // also stop the service after an OOM-killed child. Persist crash-like
4347
- // state when the process cgroup recorded an OOM kill; otherwise retain
4348
- // the clean-restart marker. Both paths mark in-flight work replayable in
4349
- // the same transaction as the marker update.
4420
+ // 3. A clean stop closes both output boundaries before persisting any
4421
+ // recovery intent. The ProcessManager receipt is the only authority for
4422
+ // which CLI turn was still active; replay rows alone cannot establish
4423
+ // that fact.
4424
+ let pmRetired = false;
4425
+ let forceCrashReason = null;
4426
+ let resumeIntents = [];
4427
+ if (shutdownObservation?.detected !== true) {
4428
+ try {
4429
+ const retirement = await prepareCleanRetirement({
4430
+ pm,
4431
+ deliveryBarrier,
4432
+ awaitHandlerSettlement,
4433
+ settlementTimeoutMs: SHUTDOWN_DRAIN_MS,
4434
+ });
4435
+ pmRetired = true;
4436
+ const projected = buildResumeIntents({
4437
+ snapshots: retirement.snapshots,
4438
+ policyVersion: CLEAN_RESUME_POLICY_VERSION,
4439
+ resolveSourceMessageId: ({ sessionKey, sourceMsgId }) => {
4440
+ const chatId = getChatIdFromKey(sessionKey);
4441
+ const messageId = db.getInboundMessageId({
4442
+ chat_id: chatId,
4443
+ msg_id: sourceMsgId,
4444
+ });
4445
+ if (!messageId) return null;
4446
+ const row = db.getInboundMessageById(messageId);
4447
+ const chatConfig = config.chats[row?.chat_id];
4448
+ const rowSessionKey = row && chatConfig
4449
+ ? getSessionKey(
4450
+ row.chat_id,
4451
+ row.thread_id == null ? null : String(row.thread_id),
4452
+ chatConfig,
4453
+ )
4454
+ : null;
4455
+ return row?.bot_name === BOT_NAME && rowSessionKey === sessionKey
4456
+ ? messageId
4457
+ : null;
4458
+ },
4459
+ });
4460
+ if (config.bot?.resumeInterruptedCliTurns === true) {
4461
+ resumeIntents = projected.resumeIntents;
4462
+ }
4463
+ for (const snapshot of projected.snapshots) {
4464
+ logEvent('clean-retirement-snapshot', {
4465
+ bot: BOT_NAME,
4466
+ session_key: snapshot.sessionKey,
4467
+ source_msg_id: snapshot.sourceMsgId ?? null,
4468
+ source_message_id: snapshot.sourceMessageId ?? null,
4469
+ policy_version: CLEAN_RESUME_POLICY_VERSION,
4470
+ eligible: snapshot.eligible === true,
4471
+ reason: snapshot.reason ?? null,
4472
+ });
4473
+ }
4474
+ } catch (err) {
4475
+ forceCrashReason = 'clean-retirement-failed';
4476
+ console.error(`[shutdown] clean retirement failed: ${err.message}`);
4477
+ if (pm) await pm.shutdown().catch(() => {});
4478
+ pmRetired = true;
4479
+ }
4480
+ }
4481
+
4482
+ // 4. Persist only after the old delivery/process boundary is closed and
4483
+ // every dispatcher-owned continuation has settled. Any uncertainty is
4484
+ // recorded crash-like, which clears stale intents and keeps ordinary
4485
+ // original-message replay available.
4350
4486
  if (db) {
4351
4487
  try {
4352
4488
  const res = persistShutdownDisposition({
4353
4489
  db,
4354
4490
  botName: BOT_NAME,
4355
4491
  observation: shutdownObservation,
4492
+ resumeIntents,
4493
+ forceCrashReason,
4356
4494
  });
4495
+ if (res.clean) {
4496
+ for (const intent of resumeIntents) {
4497
+ logEvent('clean-resume-intent-recorded', {
4498
+ bot: BOT_NAME,
4499
+ session_key: intent.sessionKey,
4500
+ source_message_id: intent.sourceMessageId,
4501
+ policy_version: intent.policyVersion,
4502
+ });
4503
+ }
4504
+ }
4357
4505
  logEvent('shutdown-drain', {
4358
4506
  bot: BOT_NAME,
4359
4507
  in_flight: remaining,
@@ -4367,10 +4515,22 @@ async function main() {
4367
4515
  console.log(`[shutdown] ${res.clean ? 'clean' : 'crash-like'} shutdown recorded (${res.shutdownReason}); ${inFlightAtSignal} in-flight at signal, drained ${drainElapsed}ms, ${remaining} still in-flight, ${res.replayMarked} marked replay-pending`);
4368
4516
  } catch (err) {
4369
4517
  console.error(`[shutdown] persistence failed: ${err.message}`);
4518
+ try {
4519
+ const fallback = db.recordCrashShutdown({ botName: BOT_NAME });
4520
+ logEvent('shutdown-persistence-fallback', {
4521
+ bot: BOT_NAME,
4522
+ replay_marked: fallback.replayMarked,
4523
+ error: err.message?.slice(0, 200),
4524
+ });
4525
+ } catch (fallbackErr) {
4526
+ console.error(
4527
+ `[shutdown] could not invalidate clean recovery state: ${fallbackErr.message}`,
4528
+ );
4529
+ }
4370
4530
  }
4371
4531
  }
4372
4532
 
4373
- // 4. Remaining shutdown: approvals sweeper, IPC, resolve hook waiters,
4533
+ // 5. Remaining shutdown: approvals sweeper, IPC, resolve hook waiters,
4374
4534
  // kill pm subprocesses, close DB.
4375
4535
  if (approvalSweepTimer) clearInterval(approvalSweepTimer);
4376
4536
  authDisabledHeartbeat?.stop(); // null if we shut down before main() armed it
@@ -4382,7 +4542,7 @@ async function main() {
4382
4542
  // old `approvalWaiters` reference here would ReferenceError
4383
4543
  // mid-shutdown and prevent pm/DB cleanup.
4384
4544
  if (cancelAllWaiters) cancelAllWaiters('cancelled', 'polygram shutting down');
4385
- if (pm) await pm.shutdown().catch(() => {});
4545
+ if (pm && !pmRetired) await pm.shutdown().catch(() => {});
4386
4546
  if (db) {
4387
4547
  try { db.logEvent('polygram-stop'); db.raw.close(); } catch {}
4388
4548
  }
@@ -4446,24 +4606,194 @@ async function main() {
4446
4606
  // window discarded it; the agent's 7-hour Xero task was abandoned.
4447
4607
  // Auto-derive: 1.2 × max(chatConfig.maxTurn) across all chats,
4448
4608
  // floored at 3 min (legacy default), capped at 2 hours (sanity).
4609
+ const cleanRecoveryTasksBySession = new Map();
4449
4610
  try {
4450
4611
  const chatIds = Object.keys(config.chats);
4451
4612
  if (chatIds.length > 0) {
4452
4613
  const replayWindowMs = resolveReplayWindowMs(config);
4453
4614
 
4454
4615
  // 0.14: classify by RESTART INTENT. Read-and-clear the clean-shutdown
4455
- // marker FIRST, in its own try/catch — ANY error => treat as crash
4456
- // (recover), never skip-all (fail toward recovery). A deliberate restart
4457
- // skips re-answering stale messages and posts one visibility notice; a
4458
- // crash recovers everything (unchanged rc.57 behavior).
4459
- let cleanShutdown = false;
4616
+ // marker and claim continuation intents FIRST. If that transaction
4617
+ // cannot commit, boot must stop: proceeding could run ordinary crash
4618
+ // replay behind an unclaimed continuation intent.
4619
+ const sourceSessionKey = (row) => {
4620
+ const chatConfig = config.chats[row?.chat_id];
4621
+ if (!row || !chatConfig) return null;
4622
+ return getSessionKey(
4623
+ row.chat_id,
4624
+ row.thread_id == null ? null : String(row.thread_id),
4625
+ chatConfig,
4626
+ );
4627
+ };
4628
+ let cleanRecovery;
4460
4629
  try {
4461
- const maxAgeMs = 2 * (replayWindowMs || 3 * 60 * 1000);
4462
- cleanShutdown = db.consumeCleanShutdownMarker({ botName: BOT_NAME, maxAgeMs }).clean;
4630
+ cleanRecovery = await startCleanRestartRecovery({
4631
+ db,
4632
+ botName: BOT_NAME,
4633
+ maxAgeMs: 2 * (replayWindowMs || 3 * 60 * 1000),
4634
+ olderThanMs: replayWindowMs || 30 * 60 * 1000,
4635
+ supportedPolicyVersions: [CLEAN_RESUME_POLICY_VERSION],
4636
+ sessionKeyForSource: sourceSessionKey,
4637
+ trackTask: trackHandlerTask,
4638
+ onClaim: (kind, claim) => {
4639
+ logEvent(
4640
+ kind === 'stranded'
4641
+ ? 'clean-resume-intent-stranded'
4642
+ : 'clean-resume-intent-claimed',
4643
+ {
4644
+ bot: claim.bot_name,
4645
+ session_key: claim.session_key,
4646
+ source_message_id: claim.source_message_id,
4647
+ policy_version: claim.policy_version,
4648
+ reason: claim.reason || undefined,
4649
+ },
4650
+ );
4651
+ },
4652
+ executeClaim: (claim, { onReady }) => executeCleanResumeClaim(claim, {
4653
+ enabled: config.bot?.resumeInterruptedCliTurns === true,
4654
+ loadSource: (messageId) => db.getInboundMessageById(messageId),
4655
+ sessionKeyForSource: sourceSessionKey,
4656
+ loadRuntimeSession: (sessionKey) => (
4657
+ db.getProviderSession(sessionKey, 'claude:channels')
4658
+ ),
4659
+ resolveStrictSpawnContext: async ({ claim: activeClaim, runtime }) => {
4660
+ try {
4661
+ return {
4662
+ ok: true,
4663
+ context: await buildSpawnContext(activeClaim.session_key, {
4664
+ strictResume: {
4665
+ expectedGenerationId: activeClaim.session_generation_id,
4666
+ expectedSessionId: runtime.provider_session_id,
4667
+ },
4668
+ }),
4669
+ };
4670
+ } catch (error) {
4671
+ return { ok: false, reason: error.code || 'session-config-drift' };
4672
+ }
4673
+ },
4674
+ strictSpawn: async ({ sessionKey, context }) => {
4675
+ const entry = await pm.getOrSpawn(sessionKey, context);
4676
+ return {
4677
+ process: entry,
4678
+ attestation: typeof entry?.getResumeAttestation === 'function'
4679
+ ? entry.getResumeAttestation()
4680
+ : entry?.resumeAttestation,
4681
+ };
4682
+ },
4683
+ discardSpawn: ({ sessionKey, process: rejectedProcess, reason }) => (
4684
+ pm.retireExpectedProcess(sessionKey, rejectedProcess, reason)
4685
+ ),
4686
+ sendContinue: ({
4687
+ sessionKey,
4688
+ text,
4689
+ sourceMsgId,
4690
+ expectedProcess,
4691
+ onDispatched,
4692
+ }) => (
4693
+ sendToProcess(sessionKey, text, createCleanResumeTurnContext({
4694
+ sourceMsgId,
4695
+ threadId: getThreadIdFromKey(sessionKey),
4696
+ }), { expectedProcess, onDispatched })
4697
+ ),
4698
+ deliverResult: async ({ claim: activeClaim, source, text }) => {
4699
+ const delivered = await processAndDeliverAgentText({
4700
+ text,
4701
+ bot,
4702
+ tg,
4703
+ chatId: source.chat_id,
4704
+ threadId: source.thread_id,
4705
+ replyToMessageId: source.msg_id,
4706
+ source: 'clean-resume-reply',
4707
+ meta: {
4708
+ botName: BOT_NAME,
4709
+ sessionKey: activeClaim.session_key,
4710
+ sourceMsgId: source.msg_id,
4711
+ },
4712
+ parseResponse,
4713
+ sanitizeAssistantReply,
4714
+ chunkMarkdownText,
4715
+ deliverReplies,
4716
+ chunkBudget: TG_CHUNK_BUDGET,
4717
+ logEvent,
4718
+ sessionKey: activeClaim.session_key,
4719
+ logger: console,
4720
+ redactInbound,
4721
+ });
4722
+ const failures = (delivered.deliverResult?.failed?.length || 0)
4723
+ + delivered.auxiliaryFailures;
4724
+ const visible = (delivered.deliverResult?.sent?.length || 0)
4725
+ + delivered.stickersSent
4726
+ + delivered.reactionsApplied;
4727
+ return {
4728
+ ok: failures === 0 && visible > 0 && !delivered.emptiedByStrategy,
4729
+ ambiguous: failures > 0 || delivered.deliveryAmbiguous,
4730
+ };
4731
+ },
4732
+ sendNotice: async ({ source, text, ambiguous }) => {
4733
+ if (!source) throw new Error('clean resume source message is missing');
4734
+ await tg(bot, 'sendMessage', {
4735
+ chat_id: Number(source.chat_id),
4736
+ text,
4737
+ ...(source.thread_id
4738
+ ? { message_thread_id: Number(source.thread_id) }
4739
+ : {}),
4740
+ }, {
4741
+ source: ambiguous
4742
+ ? 'clean-resume-uncertain-notice'
4743
+ : 'clean-resume-notice',
4744
+ botName: BOT_NAME,
4745
+ sessionKey: sourceSessionKey(source),
4746
+ sourceMsgId: source.msg_id,
4747
+ plainText: true,
4748
+ });
4749
+ },
4750
+ complete: ({ sourceMessageId, status }) => {
4751
+ const completion = db.completeCleanRestartRecovery({
4752
+ sourceMessageId,
4753
+ status,
4754
+ });
4755
+ if (completion.changes !== 1) {
4756
+ const error = new Error(
4757
+ `clean recovery source ${sourceMessageId} lost terminal ownership`,
4758
+ );
4759
+ error.code = 'CLEAN_RECOVERY_TERMINAL_CONFLICT';
4760
+ throw error;
4761
+ }
4762
+ return completion;
4763
+ },
4764
+ logEvent,
4765
+ onReady,
4766
+ }),
4767
+ onTaskError: (error, claim) => {
4768
+ logEvent('clean-resume-task-failed', {
4769
+ bot: claim.bot_name,
4770
+ session_key: claim.session_key,
4771
+ source_message_id: claim.source_message_id,
4772
+ policy_version: claim.policy_version,
4773
+ reason: error.code || 'unexpected-recovery-error',
4774
+ });
4775
+ console.error(
4776
+ `[clean-resume] ${claim.session_key || 'unknown'}: ${error.message}`,
4777
+ );
4778
+ },
4779
+ });
4780
+ for (let index = 0; index < cleanRecovery.claims.length; index += 1) {
4781
+ const sessionKey = cleanRecovery.claims[index].session_key;
4782
+ const task = cleanRecovery.tasks[index];
4783
+ const previous = cleanRecoveryTasksBySession.get(sessionKey);
4784
+ cleanRecoveryTasksBySession.set(
4785
+ sessionKey,
4786
+ previous ? Promise.all([previous, task]) : task,
4787
+ );
4788
+ }
4463
4789
  } catch (err) {
4464
- console.error(`[replay] clean-shutdown marker read failed (-> crash recover): ${err.message}`);
4465
- cleanShutdown = false;
4790
+ const fatal = new Error(
4791
+ `clean restart recovery claim failed; automated recovery is fenced: ${err.message}`,
4792
+ );
4793
+ fatal.code = 'CLEAN_RECOVERY_CLAIM_FAILED';
4794
+ throw fatal;
4466
4795
  }
4796
+ const cleanShutdown = cleanRecovery.clean;
4467
4797
 
4468
4798
  const candidates = db.getReplayCandidates({ chatIds, ...(replayWindowMs && { olderThanMs: replayWindowMs }) });
4469
4799
 
@@ -4514,16 +4844,8 @@ async function main() {
4514
4844
  return msg;
4515
4845
  };
4516
4846
 
4517
- const replaySessionKey = (row) => {
4518
- const chatConfig = config.chats[row.chat_id];
4519
- if (!chatConfig) return null;
4520
- const threadId = row.thread_id == null
4521
- ? null
4522
- : String(row.thread_id);
4523
- return getSessionKey(row.chat_id, threadId, chatConfig);
4524
- };
4525
4847
  const getProviderRecovery = (row) => {
4526
- const sessionKey = replaySessionKey(row);
4848
+ const sessionKey = sourceSessionKey(row);
4527
4849
  if (!sessionKey) {
4528
4850
  return {
4529
4851
  provider: 'unknown',
@@ -4625,6 +4947,7 @@ async function main() {
4625
4947
  }
4626
4948
  }
4627
4949
  } catch (err) {
4950
+ if (err.code === 'CLEAN_RECOVERY_CLAIM_FAILED') throw err;
4628
4951
  console.error(`[replay] boot replay failed: ${err.message}`);
4629
4952
  }
4630
4953
 
@@ -4660,9 +4983,10 @@ async function main() {
4660
4983
  }
4661
4984
  let replayed = 0;
4662
4985
  let surfacedFallback = 0;
4663
- for (const o of orphansLatest.values()) {
4986
+ let deferred = 0;
4987
+ const recoverCompact = async (o) => {
4664
4988
  const chatCfg = config.chats[o.chat_id];
4665
- if (!chatCfg) continue;
4989
+ if (!chatCfg) return;
4666
4990
  const threadId = o.thread_id ? Number(o.thread_id) : null;
4667
4991
  const savedSessionId = getClaudeSessionId(db, o.session_key);
4668
4992
 
@@ -4691,7 +5015,7 @@ async function main() {
4691
5015
  user_id: o.user_id,
4692
5016
  });
4693
5017
  replayed += 1;
4694
- continue;
5018
+ return;
4695
5019
  } catch (err) {
4696
5020
  console.error(`[compact-replay] ${o.session_key}: ${err.message} — falling back to surface`);
4697
5021
  // fall through to surface fallback below
@@ -4720,9 +5044,24 @@ async function main() {
4720
5044
  } catch (err) {
4721
5045
  console.error(`[compact-orphan-surface] ${o.session_key}: ${err.message}`);
4722
5046
  }
5047
+ };
5048
+ for (const o of orphansLatest.values()) {
5049
+ const recoveryTask = cleanRecoveryTasksBySession.get(o.session_key);
5050
+ if (recoveryTask) {
5051
+ deferred += 1;
5052
+ trackHandlerTask(
5053
+ Promise.resolve(recoveryTask)
5054
+ .then(() => recoverCompact(o))
5055
+ .catch((err) => {
5056
+ console.error(`[compact-replay] ${o.session_key}: deferred recovery failed: ${err.message}`);
5057
+ }),
5058
+ );
5059
+ continue;
5060
+ }
5061
+ await recoverCompact(o);
4723
5062
  }
4724
- if (replayed + surfacedFallback > 0) {
4725
- console.log(`[compact-orphan] silent-replayed=${replayed}, surfaced-fallback=${surfacedFallback}`);
5063
+ if (replayed + surfacedFallback + deferred > 0) {
5064
+ console.log(`[compact-orphan] silent-replayed=${replayed}, surfaced-fallback=${surfacedFallback}, deferred=${deferred}`);
4726
5065
  }
4727
5066
  } catch (err) {
4728
5067
  console.error(`[compact-orphan-handler] failed: ${err.message}`);