polygram 0.31.0 → 0.33.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');
@@ -133,7 +134,7 @@ const { chunkMarkdownText } = require('./lib/telegram/chunk');
133
134
  const {
134
135
  toTelegramRichBlocks, resolveRichTextEnabled, isRichCapabilityError, isRichContentError,
135
136
  isRichCapabilityErrorExplicit, isRichMessageFieldRejection, stripMediaMarkdown,
136
- isRichLimitError,
137
+ isRichLimitError, blocksAreStyled,
137
138
  } = require('./lib/telegram/rich');
138
139
  const {
139
140
  makeRichMediaResolver,
@@ -155,6 +156,7 @@ const { createRichSender, sendRichWithStylingRetry } = require('./lib/telegram/r
155
156
  const { createRichDeliveryFactory } = require('./lib/telegram/rich-dispatch');
156
157
  const { createRichEditStrategy } = require('./lib/telegram/rich-edit-dispatch');
157
158
  const { composeDeliverTextFactories } = require('./lib/telegram/deliver-strategy');
159
+ const { composingMarker } = require('./lib/telegram/composing-marker');
158
160
  const { buildPolygramDisplayHint } = require('./lib/telegram/display-hint');
159
161
  const { redactBotToken, stripUrlCredentials } = require('./lib/error/net');
160
162
  // F#23: shared agent-reply helper. parseResponse + sanitizer + chunked
@@ -177,6 +179,17 @@ const { createAuthDisabledGate } = require('./lib/ops/auth-disabled-gate');
177
179
  const { createHeartbeat } = require('./lib/ops/heartbeat');
178
180
  const { createCgroupOomObserver } = require('./lib/ops/cgroup-oom-observer');
179
181
  const { persistShutdownDisposition } = require('./lib/ops/shutdown-disposition');
182
+ const {
183
+ prepareCleanRetirement,
184
+ buildResumeIntents,
185
+ } = require('./lib/ops/clean-shutdown');
186
+ const {
187
+ CLEAN_RESUME_POLICY_VERSION,
188
+ createCleanResumeTurnContext,
189
+ executeCleanResumeClaim,
190
+ startCleanRestartRecovery,
191
+ validateStrictResumeSpawn,
192
+ } = require('./lib/ops/clean-resume');
180
193
  const { resolveReplayWindowMs } = require('./lib/db/replay-window');
181
194
  const { pruneEvents, resolveRetentionPolicy, validatePolicy } = require('./lib/db/events-retention');
182
195
  const { sweepSecrets, resolveSecretSweepConfig } = require('./lib/db/secret-sweep');
@@ -226,6 +239,7 @@ let emojiToSticker = {}; // emoji → file_id
226
239
  let config;
227
240
  let db;
228
241
  let tg; // unified sender, created after db opens
242
+ let deliveryBarrier = null;
229
243
  let pairings; // pairings store, created after db opens
230
244
  let approvals; // approvals store, created after db opens
231
245
  // approvalWaiters Map moved to lib/handlers/approvals.js (commit 29).
@@ -753,7 +767,10 @@ async function resolveSessionRuntimeView({
753
767
 
754
768
  async function buildSpawnContext(
755
769
  sessionKey,
756
- { mutateSessionOnDrift = true } = {},
770
+ {
771
+ mutateSessionOnDrift = true,
772
+ strictResume = null,
773
+ } = {},
757
774
  ) {
758
775
  const chatId = getChatIdFromKey(sessionKey);
759
776
  const chatConfig = config.chats[chatId];
@@ -765,6 +782,12 @@ async function buildSpawnContext(
765
782
  threadId: threadId || null,
766
783
  });
767
784
 
785
+ if (strictResume && promptBackend === 'codex') {
786
+ validateStrictResumeSpawn({
787
+ strictResume,
788
+ promptBackend,
789
+ });
790
+ }
768
791
  if (promptBackend === 'codex') {
769
792
  if (!codexRuntimeController) {
770
793
  const error = new Error('Codex runtime controller is not initialized');
@@ -809,7 +832,22 @@ async function buildSpawnContext(
809
832
  promptBackend,
810
833
  );
811
834
  let existingSessionId;
812
- if (isColdSpawn) {
835
+ if (strictResume) {
836
+ const topicConfig = getTopicConfig(chatConfig, threadId || null);
837
+ const resolved = {
838
+ agent: topicConfig.agent || chatConfig.agent || null,
839
+ cwd: topicConfig.cwd || chatConfig.cwd || null,
840
+ backend: pickBackend({ config, chatId, threadId: threadId || null, pmDefault: 'sdk' }),
841
+ };
842
+ const runtime = db.getProviderSession(sessionKey, 'claude:channels');
843
+ existingSessionId = validateStrictResumeSpawn({
844
+ strictResume,
845
+ promptBackend,
846
+ liveProcess,
847
+ runtime,
848
+ resolved,
849
+ });
850
+ } else if (isColdSpawn) {
813
851
  const topicConfig = getTopicConfig(chatConfig, threadId || null);
814
852
  const resolved = {
815
853
  agent: topicConfig.agent || chatConfig.agent || null,
@@ -843,6 +881,11 @@ async function buildSpawnContext(
843
881
  threadId: threadId || null,
844
882
  label: getSessionLabel(chatConfig, threadId),
845
883
  existingSessionId,
884
+ ...(strictResume ? {
885
+ resumePolicy: 'require-existing-session',
886
+ expectedSessionId: strictResume.expectedSessionId,
887
+ noWaitForCapacity: true,
888
+ } : {}),
846
889
  // File-send outbound cap inputs: localApi (backend ceiling) + the resolved
847
890
  // per-file override (topic → chat → bot → default) from the SAME resolver
848
891
  // the inbound filter and the send() choke point use, so CliProcess's
@@ -866,8 +909,13 @@ async function getOrSpawnForChat(sessionKey) {
866
909
  return pm.getOrSpawn(sessionKey, ctx);
867
910
  }
868
911
 
869
- async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched } = {}) {
870
- const entry = await getOrSpawnForChat(sessionKey);
912
+ async function sendToProcess(
913
+ sessionKey,
914
+ prompt,
915
+ context = {},
916
+ { onDispatched, expectedProcess = null } = {},
917
+ ) {
918
+ const entry = expectedProcess || await getOrSpawnForChat(sessionKey);
871
919
  if (!entry) throw new Error('No process for chat');
872
920
  const chatId = getChatIdFromKey(sessionKey);
873
921
  const chatConfig = config.chats[chatId];
@@ -900,6 +948,7 @@ async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched }
900
948
  maxTurnMs,
901
949
  maxTurnHardMs,
902
950
  context,
951
+ ...(expectedProcess ? { expectedProcess } : {}),
903
952
  });
904
953
  if (typeof onDispatched === 'function') onDispatched();
905
954
  return await turnP;
@@ -934,7 +983,13 @@ async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched }
934
983
  // starts, which is the correct UX (and what the user already expects).
935
984
  const release = await stdinLock.acquire(sessionKey);
936
985
  try {
937
- const turnP = pm.send(sessionKey, prompt, { timeoutMs, maxTurnMs, maxTurnHardMs, context });
986
+ const turnP = pm.send(sessionKey, prompt, {
987
+ timeoutMs,
988
+ maxTurnMs,
989
+ maxTurnHardMs,
990
+ context,
991
+ ...(expectedProcess ? { expectedProcess } : {}),
992
+ });
938
993
  // Phase 3 §4: pm.send synchronously kicks off the turn — the
939
994
  // process is now inFlight. Signal the committed-intent latch so
940
995
  // it can release; a concurrent handler will then correctly see
@@ -975,6 +1030,8 @@ let attemptAutoResume = null;
975
1030
  let errorReplyText = null;
976
1031
  let queueWarnThreshold = null;
977
1032
  let inFlightHandlers = null;
1033
+ let awaitHandlerSettlement = null;
1034
+ let trackHandlerTask = null;
978
1035
  let recoverCodexRequest = null;
979
1036
 
980
1037
  // AUTH_DISABLED (docs/AUTH_DISABLED_HANDLING_SPEC.md): dedupe/re-arm gate for
@@ -1373,7 +1430,12 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1373
1430
  await tg(bot, 'sendMessage', {
1374
1431
  chat_id: chatId, text: `Attachment(s) skipped: ${summary.slice(0, 300)}`,
1375
1432
  ...replyOpts(threadId),
1376
- }, { source: 'attachment-skipped', botName: BOT_NAME });
1433
+ }, {
1434
+ source: 'attachment-skipped',
1435
+ botName: BOT_NAME,
1436
+ sessionKey,
1437
+ sourceMsgId: msg.message_id,
1438
+ });
1377
1439
  } catch (err) {
1378
1440
  // Surface the failure: claude is about to reply as if the photo
1379
1441
  // was processed (because filterAttachments dropped it before
@@ -1416,6 +1478,8 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1416
1478
  const outMetaBase = {
1417
1479
  source: 'bot-reply-stream',
1418
1480
  botName: BOT_NAME,
1481
+ sessionKey,
1482
+ sourceMsgId: msg.message_id,
1419
1483
  model: chatConfig.model,
1420
1484
  effort: chatConfig.effort,
1421
1485
  ...(linkPreview === false ? { linkPreview: false } : {}),
@@ -1428,6 +1492,11 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1428
1492
  // vertically. After the first send, the flag flips and subsequent
1429
1493
  // initial-sends omit reply_parameters.
1430
1494
  let firstBubbleSent = false;
1495
+ // The turn id the engine assigned at dispatch, published by its onTurnId
1496
+ // callback below. Declared at turn scope rather than beside the dispatch
1497
+ // because the streamer's own instrumentation — created before the dispatch —
1498
+ // needs to name the turn a snapshot belonged to.
1499
+ let dispatchedTurnId = null;
1431
1500
  // Streaming is unconditional as of 0.4.0 — matches OpenClaw's model and
1432
1501
  // eliminates the "stuck at 15min typing" complaint from the non-streaming
1433
1502
  // code path. For short responses the streamer stays idle and we fall
@@ -1503,6 +1572,8 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1503
1572
  threadId,
1504
1573
  replyToMessageId: msg.message_id,
1505
1574
  botName: BOT_NAME,
1575
+ sessionKey,
1576
+ sourceMsgId: msg.message_id,
1506
1577
  logEvent: (kind, detail) => logEvent(kind, {
1507
1578
  chat_id: chatId, thread_id: threadId, bot: BOT_NAME, ...detail,
1508
1579
  }),
@@ -1533,11 +1604,30 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1533
1604
  // captures them. See stripInlineTagsForStreamer above.
1534
1605
  transformText: stripInlineTagsForStreamer,
1535
1606
  toRichPayload,
1607
+ // "Still writing" on the growing bubble. The marker matches the styling
1608
+ // of what it decorates rather than what the transport can carry: rich-edit
1609
+ // records a styling verdict for any payload blocksAreStyled() calls
1610
+ // styled, and a typed node on flat content would make those verdicts —
1611
+ // strike runs reset, flatten retries paid — be about polygram's own
1612
+ // decoration. Reading the latch instead is not enough: it says what is
1613
+ // POSSIBLE, and the content is what is actually there.
1614
+ toComposingMarker: ({ blocks }) => composingMarker({ styled: blocksAreStyled(blocks || []) }),
1536
1615
  onRichUpgrade: () => {
1537
1616
  // Record the visible transition when an existing plain bubble
1538
1617
  // becomes rich during streaming.
1539
1618
  logEvent('rich-streaming-upgrade', { chat_id: chatId, thread_id: threadId, bot: BOT_NAME });
1540
1619
  },
1620
+ // A snapshot that lost text the reader had already seen. The streamer
1621
+ // refused it and kept the bubble on the last good one; this is the only
1622
+ // place that misuse becomes visible, since the tool ack stays ok either
1623
+ // way (an error there buys a retry loop, not a better snapshot).
1624
+ onNonCumulativeSnapshot: ({ prevLen, newLen }) => {
1625
+ logEvent('stream-noncumulative', {
1626
+ chat_id: chatId, thread_id: threadId, bot: BOT_NAME,
1627
+ session_key: sessionKey, turn_id: dispatchedTurnId,
1628
+ prev_len: prevLen, new_len: newLen,
1629
+ });
1630
+ },
1541
1631
  send: async (payload) => {
1542
1632
  const openRich = payload && typeof payload === 'object' && payload.rich;
1543
1633
  const hadReplyAnchor = !firstBubbleSent;
@@ -1609,6 +1699,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1609
1699
  phase: payload.phase,
1610
1700
  mediaContext,
1611
1701
  hadReplyAnchor: payload.hadReplyAnchor,
1702
+ meta: outMetaBase,
1612
1703
  });
1613
1704
  }
1614
1705
  const text = payload;
@@ -1623,7 +1714,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1623
1714
  chat_id: chatId,
1624
1715
  message_id: messageId,
1625
1716
  text: sanitizeLiveText(text),
1626
- }, { source: 'bot-reply-stream-edit', botName: BOT_NAME });
1717
+ }, { ...outMetaBase, source: 'bot-reply-stream-edit' });
1627
1718
  } catch (err) {
1628
1719
  // Stream-edit failures would otherwise be invisible — edits
1629
1720
  // don't insert a messages row by default (tg() does, but we
@@ -1648,7 +1739,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1648
1739
  try {
1649
1740
  await tg(bot, 'deleteMessage', {
1650
1741
  chat_id: chatId, message_id: messageId,
1651
- }, { source: 'bot-reply-stream-discard', botName: BOT_NAME });
1742
+ }, { ...outMetaBase, source: 'bot-reply-stream-discard' });
1652
1743
  } catch (err) {
1653
1744
  console.error(`[${label}] discard preview failed: ${err.message}`);
1654
1745
  throw err;
@@ -2084,7 +2175,6 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
2084
2175
  // dispatchedTurnId is filled by the engine's onTurnId callback below,
2085
2176
  // before any tool call for this turn can reach us; it lets a reply be
2086
2177
  // checked against the turn it actually names.
2087
- let dispatchedTurnId = null;
2088
2178
  if (streamPreviewEnabled) {
2089
2179
  releaseStreamerRegistration = streamerRegistry.register(sessionKey, {
2090
2180
  streamer, chatId, deliveredTexts,
@@ -2578,7 +2668,12 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
2578
2668
  chat_id: chatId,
2579
2669
  message_id: msg.message_id,
2580
2670
  reaction: [{ type: 'emoji', emoji }],
2581
- }, { ...outMeta, source: 'inline-reaction', reaction: emoji });
2671
+ }, {
2672
+ ...outMeta,
2673
+ deliveryClass: 'reply-bearing',
2674
+ source: 'inline-reaction',
2675
+ reaction: emoji,
2676
+ });
2582
2677
  } catch (err) {
2583
2678
  console.error(`[${label}] inline setMessageReaction(${emoji}) failed: ${err.message}`);
2584
2679
  }
@@ -2705,7 +2800,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
2705
2800
  chat_id: chatId,
2706
2801
  message_id: msg.message_id,
2707
2802
  reaction: [{ type: 'emoji', emoji: parsed.reaction }],
2708
- }, outMeta).catch((err) => {
2803
+ }, { ...outMeta, deliveryClass: 'reply-bearing' }).catch((err) => {
2709
2804
  telegramDeliveryComplete = false;
2710
2805
  console.error(`[${label}] setMessageReaction failed: ${err.message}`);
2711
2806
  });
@@ -3447,7 +3542,8 @@ async function main() {
3447
3542
  );
3448
3543
  }
3449
3544
  }
3450
- tg = createSender(db, console, config);
3545
+ deliveryBarrier = createDeliveryBarrier();
3546
+ tg = createSender(db, console, config, deliveryBarrier);
3451
3547
  richMediaFileIdCache = createMediaFileIdCache();
3452
3548
  // ONE latch decision for both rich paths. They set the same flag, so a
3453
3549
  // counter owned by either would let that path latch on its first bare
@@ -3712,6 +3808,7 @@ async function main() {
3712
3808
 
3713
3809
  const sdkCallbacks = createSdkCallbacks({
3714
3810
  db, dbWrite, config, bot, botName: BOT_NAME, tg, logEvent, sessionFeedback,
3811
+ deliveryBarrier,
3715
3812
  classifyToolName, announce, shouldAnnounce, contextHintShown,
3716
3813
  extractAssistantText, getChatIdFromKey, getThreadIdFromKey,
3717
3814
  // F#23: enable parse/sanitize/sticker/react on the autonomous-wakeup path.
@@ -4131,6 +4228,8 @@ async function main() {
4131
4228
  errorReplyText,
4132
4229
  queueWarnThreshold,
4133
4230
  inFlightHandlers,
4231
+ awaitSettlement: awaitHandlerSettlement,
4232
+ trackTask: trackHandlerTask,
4134
4233
  } = createDispatcher({
4135
4234
  config, db, dbWrite, tg, botName: BOT_NAME, logEvent,
4136
4235
  handleMessage, sendToProcess,
@@ -4342,18 +4441,91 @@ async function main() {
4342
4441
  const drainElapsed = Date.now() - drainStart;
4343
4442
  const remaining = countInFlight(inFlightHandlers);
4344
4443
 
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.
4444
+ // 3. A clean stop closes both output boundaries before persisting any
4445
+ // recovery intent. The ProcessManager receipt is the only authority for
4446
+ // which CLI turn was still active; replay rows alone cannot establish
4447
+ // that fact.
4448
+ let pmRetired = false;
4449
+ let forceCrashReason = null;
4450
+ let resumeIntents = [];
4451
+ if (shutdownObservation?.detected !== true) {
4452
+ try {
4453
+ const retirement = await prepareCleanRetirement({
4454
+ pm,
4455
+ deliveryBarrier,
4456
+ awaitHandlerSettlement,
4457
+ settlementTimeoutMs: SHUTDOWN_DRAIN_MS,
4458
+ });
4459
+ pmRetired = true;
4460
+ const projected = buildResumeIntents({
4461
+ snapshots: retirement.snapshots,
4462
+ policyVersion: CLEAN_RESUME_POLICY_VERSION,
4463
+ resolveSourceMessageId: ({ sessionKey, sourceMsgId }) => {
4464
+ const chatId = getChatIdFromKey(sessionKey);
4465
+ const messageId = db.getInboundMessageId({
4466
+ chat_id: chatId,
4467
+ msg_id: sourceMsgId,
4468
+ });
4469
+ if (!messageId) return null;
4470
+ const row = db.getInboundMessageById(messageId);
4471
+ const chatConfig = config.chats[row?.chat_id];
4472
+ const rowSessionKey = row && chatConfig
4473
+ ? getSessionKey(
4474
+ row.chat_id,
4475
+ row.thread_id == null ? null : String(row.thread_id),
4476
+ chatConfig,
4477
+ )
4478
+ : null;
4479
+ return row?.bot_name === BOT_NAME && rowSessionKey === sessionKey
4480
+ ? messageId
4481
+ : null;
4482
+ },
4483
+ });
4484
+ if (config.bot?.resumeInterruptedCliTurns === true) {
4485
+ resumeIntents = projected.resumeIntents;
4486
+ }
4487
+ for (const snapshot of projected.snapshots) {
4488
+ logEvent('clean-retirement-snapshot', {
4489
+ bot: BOT_NAME,
4490
+ session_key: snapshot.sessionKey,
4491
+ source_msg_id: snapshot.sourceMsgId ?? null,
4492
+ source_message_id: snapshot.sourceMessageId ?? null,
4493
+ policy_version: CLEAN_RESUME_POLICY_VERSION,
4494
+ eligible: snapshot.eligible === true,
4495
+ reason: snapshot.reason ?? null,
4496
+ });
4497
+ }
4498
+ } catch (err) {
4499
+ forceCrashReason = 'clean-retirement-failed';
4500
+ console.error(`[shutdown] clean retirement failed: ${err.message}`);
4501
+ if (pm) await pm.shutdown().catch(() => {});
4502
+ pmRetired = true;
4503
+ }
4504
+ }
4505
+
4506
+ // 4. Persist only after the old delivery/process boundary is closed and
4507
+ // every dispatcher-owned continuation has settled. Any uncertainty is
4508
+ // recorded crash-like, which clears stale intents and keeps ordinary
4509
+ // original-message replay available.
4350
4510
  if (db) {
4351
4511
  try {
4352
4512
  const res = persistShutdownDisposition({
4353
4513
  db,
4354
4514
  botName: BOT_NAME,
4355
4515
  observation: shutdownObservation,
4516
+ resumeIntents,
4517
+ forceCrashReason,
4356
4518
  });
4519
+ if (res.clean) {
4520
+ for (const intent of resumeIntents) {
4521
+ logEvent('clean-resume-intent-recorded', {
4522
+ bot: BOT_NAME,
4523
+ session_key: intent.sessionKey,
4524
+ source_message_id: intent.sourceMessageId,
4525
+ policy_version: intent.policyVersion,
4526
+ });
4527
+ }
4528
+ }
4357
4529
  logEvent('shutdown-drain', {
4358
4530
  bot: BOT_NAME,
4359
4531
  in_flight: remaining,
@@ -4367,10 +4539,22 @@ async function main() {
4367
4539
  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
4540
  } catch (err) {
4369
4541
  console.error(`[shutdown] persistence failed: ${err.message}`);
4542
+ try {
4543
+ const fallback = db.recordCrashShutdown({ botName: BOT_NAME });
4544
+ logEvent('shutdown-persistence-fallback', {
4545
+ bot: BOT_NAME,
4546
+ replay_marked: fallback.replayMarked,
4547
+ error: err.message?.slice(0, 200),
4548
+ });
4549
+ } catch (fallbackErr) {
4550
+ console.error(
4551
+ `[shutdown] could not invalidate clean recovery state: ${fallbackErr.message}`,
4552
+ );
4553
+ }
4370
4554
  }
4371
4555
  }
4372
4556
 
4373
- // 4. Remaining shutdown: approvals sweeper, IPC, resolve hook waiters,
4557
+ // 5. Remaining shutdown: approvals sweeper, IPC, resolve hook waiters,
4374
4558
  // kill pm subprocesses, close DB.
4375
4559
  if (approvalSweepTimer) clearInterval(approvalSweepTimer);
4376
4560
  authDisabledHeartbeat?.stop(); // null if we shut down before main() armed it
@@ -4382,7 +4566,7 @@ async function main() {
4382
4566
  // old `approvalWaiters` reference here would ReferenceError
4383
4567
  // mid-shutdown and prevent pm/DB cleanup.
4384
4568
  if (cancelAllWaiters) cancelAllWaiters('cancelled', 'polygram shutting down');
4385
- if (pm) await pm.shutdown().catch(() => {});
4569
+ if (pm && !pmRetired) await pm.shutdown().catch(() => {});
4386
4570
  if (db) {
4387
4571
  try { db.logEvent('polygram-stop'); db.raw.close(); } catch {}
4388
4572
  }
@@ -4446,24 +4630,194 @@ async function main() {
4446
4630
  // window discarded it; the agent's 7-hour Xero task was abandoned.
4447
4631
  // Auto-derive: 1.2 × max(chatConfig.maxTurn) across all chats,
4448
4632
  // floored at 3 min (legacy default), capped at 2 hours (sanity).
4633
+ const cleanRecoveryTasksBySession = new Map();
4449
4634
  try {
4450
4635
  const chatIds = Object.keys(config.chats);
4451
4636
  if (chatIds.length > 0) {
4452
4637
  const replayWindowMs = resolveReplayWindowMs(config);
4453
4638
 
4454
4639
  // 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;
4640
+ // marker and claim continuation intents FIRST. If that transaction
4641
+ // cannot commit, boot must stop: proceeding could run ordinary crash
4642
+ // replay behind an unclaimed continuation intent.
4643
+ const sourceSessionKey = (row) => {
4644
+ const chatConfig = config.chats[row?.chat_id];
4645
+ if (!row || !chatConfig) return null;
4646
+ return getSessionKey(
4647
+ row.chat_id,
4648
+ row.thread_id == null ? null : String(row.thread_id),
4649
+ chatConfig,
4650
+ );
4651
+ };
4652
+ let cleanRecovery;
4460
4653
  try {
4461
- const maxAgeMs = 2 * (replayWindowMs || 3 * 60 * 1000);
4462
- cleanShutdown = db.consumeCleanShutdownMarker({ botName: BOT_NAME, maxAgeMs }).clean;
4654
+ cleanRecovery = await startCleanRestartRecovery({
4655
+ db,
4656
+ botName: BOT_NAME,
4657
+ maxAgeMs: 2 * (replayWindowMs || 3 * 60 * 1000),
4658
+ olderThanMs: replayWindowMs || 30 * 60 * 1000,
4659
+ supportedPolicyVersions: [CLEAN_RESUME_POLICY_VERSION],
4660
+ sessionKeyForSource: sourceSessionKey,
4661
+ trackTask: trackHandlerTask,
4662
+ onClaim: (kind, claim) => {
4663
+ logEvent(
4664
+ kind === 'stranded'
4665
+ ? 'clean-resume-intent-stranded'
4666
+ : 'clean-resume-intent-claimed',
4667
+ {
4668
+ bot: claim.bot_name,
4669
+ session_key: claim.session_key,
4670
+ source_message_id: claim.source_message_id,
4671
+ policy_version: claim.policy_version,
4672
+ reason: claim.reason || undefined,
4673
+ },
4674
+ );
4675
+ },
4676
+ executeClaim: (claim, { onReady }) => executeCleanResumeClaim(claim, {
4677
+ enabled: config.bot?.resumeInterruptedCliTurns === true,
4678
+ loadSource: (messageId) => db.getInboundMessageById(messageId),
4679
+ sessionKeyForSource: sourceSessionKey,
4680
+ loadRuntimeSession: (sessionKey) => (
4681
+ db.getProviderSession(sessionKey, 'claude:channels')
4682
+ ),
4683
+ resolveStrictSpawnContext: async ({ claim: activeClaim, runtime }) => {
4684
+ try {
4685
+ return {
4686
+ ok: true,
4687
+ context: await buildSpawnContext(activeClaim.session_key, {
4688
+ strictResume: {
4689
+ expectedGenerationId: activeClaim.session_generation_id,
4690
+ expectedSessionId: runtime.provider_session_id,
4691
+ },
4692
+ }),
4693
+ };
4694
+ } catch (error) {
4695
+ return { ok: false, reason: error.code || 'session-config-drift' };
4696
+ }
4697
+ },
4698
+ strictSpawn: async ({ sessionKey, context }) => {
4699
+ const entry = await pm.getOrSpawn(sessionKey, context);
4700
+ return {
4701
+ process: entry,
4702
+ attestation: typeof entry?.getResumeAttestation === 'function'
4703
+ ? entry.getResumeAttestation()
4704
+ : entry?.resumeAttestation,
4705
+ };
4706
+ },
4707
+ discardSpawn: ({ sessionKey, process: rejectedProcess, reason }) => (
4708
+ pm.retireExpectedProcess(sessionKey, rejectedProcess, reason)
4709
+ ),
4710
+ sendContinue: ({
4711
+ sessionKey,
4712
+ text,
4713
+ sourceMsgId,
4714
+ expectedProcess,
4715
+ onDispatched,
4716
+ }) => (
4717
+ sendToProcess(sessionKey, text, createCleanResumeTurnContext({
4718
+ sourceMsgId,
4719
+ threadId: getThreadIdFromKey(sessionKey),
4720
+ }), { expectedProcess, onDispatched })
4721
+ ),
4722
+ deliverResult: async ({ claim: activeClaim, source, text }) => {
4723
+ const delivered = await processAndDeliverAgentText({
4724
+ text,
4725
+ bot,
4726
+ tg,
4727
+ chatId: source.chat_id,
4728
+ threadId: source.thread_id,
4729
+ replyToMessageId: source.msg_id,
4730
+ source: 'clean-resume-reply',
4731
+ meta: {
4732
+ botName: BOT_NAME,
4733
+ sessionKey: activeClaim.session_key,
4734
+ sourceMsgId: source.msg_id,
4735
+ },
4736
+ parseResponse,
4737
+ sanitizeAssistantReply,
4738
+ chunkMarkdownText,
4739
+ deliverReplies,
4740
+ chunkBudget: TG_CHUNK_BUDGET,
4741
+ logEvent,
4742
+ sessionKey: activeClaim.session_key,
4743
+ logger: console,
4744
+ redactInbound,
4745
+ });
4746
+ const failures = (delivered.deliverResult?.failed?.length || 0)
4747
+ + delivered.auxiliaryFailures;
4748
+ const visible = (delivered.deliverResult?.sent?.length || 0)
4749
+ + delivered.stickersSent
4750
+ + delivered.reactionsApplied;
4751
+ return {
4752
+ ok: failures === 0 && visible > 0 && !delivered.emptiedByStrategy,
4753
+ ambiguous: failures > 0 || delivered.deliveryAmbiguous,
4754
+ };
4755
+ },
4756
+ sendNotice: async ({ source, text, ambiguous }) => {
4757
+ if (!source) throw new Error('clean resume source message is missing');
4758
+ await tg(bot, 'sendMessage', {
4759
+ chat_id: Number(source.chat_id),
4760
+ text,
4761
+ ...(source.thread_id
4762
+ ? { message_thread_id: Number(source.thread_id) }
4763
+ : {}),
4764
+ }, {
4765
+ source: ambiguous
4766
+ ? 'clean-resume-uncertain-notice'
4767
+ : 'clean-resume-notice',
4768
+ botName: BOT_NAME,
4769
+ sessionKey: sourceSessionKey(source),
4770
+ sourceMsgId: source.msg_id,
4771
+ plainText: true,
4772
+ });
4773
+ },
4774
+ complete: ({ sourceMessageId, status }) => {
4775
+ const completion = db.completeCleanRestartRecovery({
4776
+ sourceMessageId,
4777
+ status,
4778
+ });
4779
+ if (completion.changes !== 1) {
4780
+ const error = new Error(
4781
+ `clean recovery source ${sourceMessageId} lost terminal ownership`,
4782
+ );
4783
+ error.code = 'CLEAN_RECOVERY_TERMINAL_CONFLICT';
4784
+ throw error;
4785
+ }
4786
+ return completion;
4787
+ },
4788
+ logEvent,
4789
+ onReady,
4790
+ }),
4791
+ onTaskError: (error, claim) => {
4792
+ logEvent('clean-resume-task-failed', {
4793
+ bot: claim.bot_name,
4794
+ session_key: claim.session_key,
4795
+ source_message_id: claim.source_message_id,
4796
+ policy_version: claim.policy_version,
4797
+ reason: error.code || 'unexpected-recovery-error',
4798
+ });
4799
+ console.error(
4800
+ `[clean-resume] ${claim.session_key || 'unknown'}: ${error.message}`,
4801
+ );
4802
+ },
4803
+ });
4804
+ for (let index = 0; index < cleanRecovery.claims.length; index += 1) {
4805
+ const sessionKey = cleanRecovery.claims[index].session_key;
4806
+ const task = cleanRecovery.tasks[index];
4807
+ const previous = cleanRecoveryTasksBySession.get(sessionKey);
4808
+ cleanRecoveryTasksBySession.set(
4809
+ sessionKey,
4810
+ previous ? Promise.all([previous, task]) : task,
4811
+ );
4812
+ }
4463
4813
  } catch (err) {
4464
- console.error(`[replay] clean-shutdown marker read failed (-> crash recover): ${err.message}`);
4465
- cleanShutdown = false;
4814
+ const fatal = new Error(
4815
+ `clean restart recovery claim failed; automated recovery is fenced: ${err.message}`,
4816
+ );
4817
+ fatal.code = 'CLEAN_RECOVERY_CLAIM_FAILED';
4818
+ throw fatal;
4466
4819
  }
4820
+ const cleanShutdown = cleanRecovery.clean;
4467
4821
 
4468
4822
  const candidates = db.getReplayCandidates({ chatIds, ...(replayWindowMs && { olderThanMs: replayWindowMs }) });
4469
4823
 
@@ -4514,16 +4868,8 @@ async function main() {
4514
4868
  return msg;
4515
4869
  };
4516
4870
 
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
4871
  const getProviderRecovery = (row) => {
4526
- const sessionKey = replaySessionKey(row);
4872
+ const sessionKey = sourceSessionKey(row);
4527
4873
  if (!sessionKey) {
4528
4874
  return {
4529
4875
  provider: 'unknown',
@@ -4625,6 +4971,7 @@ async function main() {
4625
4971
  }
4626
4972
  }
4627
4973
  } catch (err) {
4974
+ if (err.code === 'CLEAN_RECOVERY_CLAIM_FAILED') throw err;
4628
4975
  console.error(`[replay] boot replay failed: ${err.message}`);
4629
4976
  }
4630
4977
 
@@ -4660,9 +5007,10 @@ async function main() {
4660
5007
  }
4661
5008
  let replayed = 0;
4662
5009
  let surfacedFallback = 0;
4663
- for (const o of orphansLatest.values()) {
5010
+ let deferred = 0;
5011
+ const recoverCompact = async (o) => {
4664
5012
  const chatCfg = config.chats[o.chat_id];
4665
- if (!chatCfg) continue;
5013
+ if (!chatCfg) return;
4666
5014
  const threadId = o.thread_id ? Number(o.thread_id) : null;
4667
5015
  const savedSessionId = getClaudeSessionId(db, o.session_key);
4668
5016
 
@@ -4691,7 +5039,7 @@ async function main() {
4691
5039
  user_id: o.user_id,
4692
5040
  });
4693
5041
  replayed += 1;
4694
- continue;
5042
+ return;
4695
5043
  } catch (err) {
4696
5044
  console.error(`[compact-replay] ${o.session_key}: ${err.message} — falling back to surface`);
4697
5045
  // fall through to surface fallback below
@@ -4720,9 +5068,24 @@ async function main() {
4720
5068
  } catch (err) {
4721
5069
  console.error(`[compact-orphan-surface] ${o.session_key}: ${err.message}`);
4722
5070
  }
5071
+ };
5072
+ for (const o of orphansLatest.values()) {
5073
+ const recoveryTask = cleanRecoveryTasksBySession.get(o.session_key);
5074
+ if (recoveryTask) {
5075
+ deferred += 1;
5076
+ trackHandlerTask(
5077
+ Promise.resolve(recoveryTask)
5078
+ .then(() => recoverCompact(o))
5079
+ .catch((err) => {
5080
+ console.error(`[compact-replay] ${o.session_key}: deferred recovery failed: ${err.message}`);
5081
+ }),
5082
+ );
5083
+ continue;
5084
+ }
5085
+ await recoverCompact(o);
4723
5086
  }
4724
- if (replayed + surfacedFallback > 0) {
4725
- console.log(`[compact-orphan] silent-replayed=${replayed}, surfaced-fallback=${surfacedFallback}`);
5087
+ if (replayed + surfacedFallback + deferred > 0) {
5088
+ console.log(`[compact-orphan] silent-replayed=${replayed}, surfaced-fallback=${surfacedFallback}, deferred=${deferred}`);
4726
5089
  }
4727
5090
  } catch (err) {
4728
5091
  console.error(`[compact-orphan-handler] failed: ${err.message}`);