polygram 0.30.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
@@ -72,6 +72,11 @@ const {
72
72
  const {
73
73
  buildCodexSpawnContext,
74
74
  } = require('./lib/codex/spawn-context');
75
+ const {
76
+ processMatchesRuntime,
77
+ resolveCodexRuntimeCandidate,
78
+ resolveRuntimeDescriptor,
79
+ } = require('./lib/runtime-config');
75
80
  const {
76
81
  scheduleCodexRetention,
77
82
  } = require('./lib/codex/retention-scheduler');
@@ -114,6 +119,7 @@ const { formatContextReply, maybeContextFullHint } = require('./lib/context-form
114
119
  const { createAbortGrace } = require('./lib/abort-grace');
115
120
  const agentLoader = require('./lib/agents/loader');
116
121
  const { createSender } = require('./lib/telegram/api');
122
+ const { createDeliveryBarrier } = require('./lib/telegram/delivery-barrier');
117
123
  const { createAsyncLock } = require('@shumkov/orchestra');
118
124
  const { sweepInbox } = require('./lib/db/inbox');
119
125
  const { parseBotArg, parseDbArg, filterConfigToBot, activeBotConfig } = require('./lib/config-scope');
@@ -172,6 +178,17 @@ const { createAuthDisabledGate } = require('./lib/ops/auth-disabled-gate');
172
178
  const { createHeartbeat } = require('./lib/ops/heartbeat');
173
179
  const { createCgroupOomObserver } = require('./lib/ops/cgroup-oom-observer');
174
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');
175
192
  const { resolveReplayWindowMs } = require('./lib/db/replay-window');
176
193
  const { pruneEvents, resolveRetentionPolicy, validatePolicy } = require('./lib/db/events-retention');
177
194
  const { sweepSecrets, resolveSecretSweepConfig } = require('./lib/db/secret-sweep');
@@ -221,6 +238,7 @@ let emojiToSticker = {}; // emoji → file_id
221
238
  let config;
222
239
  let db;
223
240
  let tg; // unified sender, created after db opens
241
+ let deliveryBarrier = null;
224
242
  let pairings; // pairings store, created after db opens
225
243
  let approvals; // approvals store, created after db opens
226
244
  // approvalWaiters Map moved to lib/handlers/approvals.js (commit 29).
@@ -599,58 +617,160 @@ async function clearAutosteeredReactions(sessionKey) {
599
617
  let buildSdkOptions = null;
600
618
  let codexRuntimeController = null;
601
619
 
620
+ function buildUnavailableCodexRuntimeView({
621
+ runtimeSelection,
622
+ chatId,
623
+ threadId,
624
+ error,
625
+ runtimeView = null,
626
+ }) {
627
+ let candidate = null;
628
+ try {
629
+ candidate = resolveCodexRuntimeCandidate({
630
+ config,
631
+ chatId,
632
+ threadId,
633
+ });
634
+ } catch {
635
+ // The runtime button must remain available even when the saved Codex
636
+ // model, effort, or cwd is incomplete.
637
+ }
638
+ const model = runtimeView?.model ?? candidate?.model ?? null;
639
+ const effort = runtimeView?.effort ?? candidate?.effort ?? null;
640
+ const desiredSettings = (
641
+ typeof model === 'string'
642
+ && typeof effort === 'string'
643
+ ) ? Object.freeze({ model, effort }) : null;
644
+ return Object.freeze({
645
+ ...runtimeView,
646
+ runtime: 'codex',
647
+ backend: runtimeSelection.backend,
648
+ configuredPm: runtimeSelection.configuredPm,
649
+ selectionSource: runtimeSelection.source,
650
+ model,
651
+ effort,
652
+ models: runtimeView?.models ?? Object.freeze([]),
653
+ efforts: runtimeView?.efforts ?? Object.freeze([]),
654
+ desiredSettings: runtimeView?.desiredSettings ?? desiredSettings,
655
+ nextTurnSettings: runtimeView?.nextTurnSettings ?? null,
656
+ observedThreadSettings: runtimeView?.observedThreadSettings ?? null,
657
+ activeTurnSettings: runtimeView?.activeTurnSettings ?? null,
658
+ processStatus: 'unavailable',
659
+ unavailableReason:
660
+ error?.code || error?.name || 'CODEX_RUNTIME_UNAVAILABLE',
661
+ });
662
+ }
663
+
602
664
  async function resolveSessionRuntimeView({
603
665
  sessionKey,
604
666
  chatId,
605
667
  threadId = null,
606
668
  } = {}) {
607
- if (codexRuntimeController) {
608
- const runtimeView = await codexRuntimeController.resolveRuntimeView({
669
+ const runtimeSelection = resolveRuntimeDescriptor({
670
+ config,
671
+ chatId,
672
+ threadId,
673
+ defaultPm: 'sdk',
674
+ logger: console,
675
+ });
676
+ const runtimeMetadata = {
677
+ runtime: runtimeSelection.runtime,
678
+ backend: runtimeSelection.backend,
679
+ configuredPm: runtimeSelection.configuredPm,
680
+ selectionSource: runtimeSelection.source,
681
+ };
682
+ if (runtimeSelection.runtime !== 'codex') {
683
+ return Object.freeze(runtimeMetadata);
684
+ }
685
+ if (!codexRuntimeController) {
686
+ return buildUnavailableCodexRuntimeView({
687
+ runtimeSelection,
688
+ chatId,
689
+ threadId,
690
+ error: { code: 'CODEX_RUNTIME_UNAVAILABLE' },
691
+ });
692
+ }
693
+
694
+ let runtimeView;
695
+ try {
696
+ runtimeView = await codexRuntimeController.resolveRuntimeView({
609
697
  sessionKey,
610
698
  chatId,
611
699
  threadId,
612
700
  });
613
- if (!isCodexRuntimeView(runtimeView)) return runtimeView;
614
- if (typeof pm?.getModelSettingsStatus !== 'function') {
615
- const error = new Error('Codex model-settings status is unavailable');
616
- error.code = 'CODEX_RUNTIME_UNAVAILABLE';
617
- throw error;
618
- }
619
- const settingsStatus = await pm.getModelSettingsStatus(sessionKey);
620
- const processStatus = settingsStatus.outcome;
621
- const unavailableReason = settingsStatus.reason ?? null;
622
- const copySettings = (settings) => (
623
- settings
624
- && typeof settings.model === 'string'
625
- && typeof settings.effort === 'string'
626
- ? Object.freeze({
627
- model: settings.model,
628
- effort: settings.effort,
629
- })
630
- : null
631
- );
632
- return Object.freeze({
633
- ...runtimeView,
634
- desiredSettings: Object.freeze({
635
- model: runtimeView.model,
636
- effort: runtimeView.effort,
637
- }),
638
- nextTurnSettings: copySettings(settingsStatus.nextTurn),
639
- observedThreadSettings: copySettings(settingsStatus.observedThread),
640
- activeTurnSettings: copySettings(settingsStatus.currentTurn),
641
- processStatus,
642
- unavailableReason,
701
+ } catch (error) {
702
+ return buildUnavailableCodexRuntimeView({
703
+ runtimeSelection,
704
+ chatId,
705
+ threadId,
706
+ error,
643
707
  });
644
708
  }
645
- if (resolvePromptBackend({ config, chatId, threadId }) === 'codex') {
646
- const error = new Error('Codex runtime controller is not initialized');
647
- error.code = 'CODEX_RUNTIME_UNAVAILABLE';
648
- throw error;
709
+ if (!isCodexRuntimeView(runtimeView)) {
710
+ return buildUnavailableCodexRuntimeView({
711
+ runtimeSelection,
712
+ chatId,
713
+ threadId,
714
+ error: { code: 'CODEX_RUNTIME_VIEW_MISMATCH' },
715
+ runtimeView,
716
+ });
717
+ }
718
+ runtimeView = Object.freeze({
719
+ ...runtimeView,
720
+ ...runtimeMetadata,
721
+ });
722
+ if (typeof pm?.getModelSettingsStatus !== 'function') {
723
+ return buildUnavailableCodexRuntimeView({
724
+ runtimeSelection,
725
+ chatId,
726
+ threadId,
727
+ error: { code: 'CODEX_RUNTIME_UNAVAILABLE' },
728
+ runtimeView,
729
+ });
649
730
  }
650
- return Object.freeze({ runtime: 'claude' });
731
+ let settingsStatus;
732
+ try {
733
+ settingsStatus = await pm.getModelSettingsStatus(sessionKey);
734
+ } catch (error) {
735
+ return buildUnavailableCodexRuntimeView({
736
+ runtimeSelection,
737
+ chatId,
738
+ threadId,
739
+ error,
740
+ runtimeView,
741
+ });
742
+ }
743
+ const copySettings = (settings) => (
744
+ settings
745
+ && typeof settings.model === 'string'
746
+ && typeof settings.effort === 'string'
747
+ ? Object.freeze({
748
+ model: settings.model,
749
+ effort: settings.effort,
750
+ })
751
+ : null
752
+ );
753
+ return Object.freeze({
754
+ ...runtimeView,
755
+ desiredSettings: Object.freeze({
756
+ model: runtimeView.model,
757
+ effort: runtimeView.effort,
758
+ }),
759
+ nextTurnSettings: copySettings(settingsStatus.nextTurn),
760
+ observedThreadSettings: copySettings(settingsStatus.observedThread),
761
+ activeTurnSettings: copySettings(settingsStatus.currentTurn),
762
+ processStatus: settingsStatus.outcome,
763
+ unavailableReason: settingsStatus.reason ?? null,
764
+ });
651
765
  }
652
766
 
653
- async function buildSpawnContext(sessionKey) {
767
+ async function buildSpawnContext(
768
+ sessionKey,
769
+ {
770
+ mutateSessionOnDrift = true,
771
+ strictResume = null,
772
+ } = {},
773
+ ) {
654
774
  const chatId = getChatIdFromKey(sessionKey);
655
775
  const chatConfig = config.chats[chatId];
656
776
  if (!chatConfig) return null;
@@ -661,6 +781,12 @@ async function buildSpawnContext(sessionKey) {
661
781
  threadId: threadId || null,
662
782
  });
663
783
 
784
+ if (strictResume && promptBackend === 'codex') {
785
+ validateStrictResumeSpawn({
786
+ strictResume,
787
+ promptBackend,
788
+ });
789
+ }
664
790
  if (promptBackend === 'codex') {
665
791
  if (!codexRuntimeController) {
666
792
  const error = new Error('Codex runtime controller is not initialized');
@@ -677,6 +803,7 @@ async function buildSpawnContext(sessionKey) {
677
803
  runtimeController: codexRuntimeController,
678
804
  getSessionLabel,
679
805
  logEvent,
806
+ mutateSessionOnDrift,
680
807
  });
681
808
  }
682
809
 
@@ -684,33 +811,54 @@ async function buildSpawnContext(sessionKey) {
684
811
  // under. agent / cwd are spawn-identity — baked into the process at
685
812
  // spawn time, never mutable on a live session. Resolve them the
686
813
  // same way the backends do (topic override merged over chat-level)
687
- // and compare to the stored `sessions` row. On drift,
688
- // resolveSessionForSpawn drops the stale row and returns
689
- // existingSessionId:null → the spawn starts fresh under the correct
690
- // config instead of `--resume`-ing a stale one. This self-heals the
691
- // pre-per-topic-config rows (e.g. shumorobot's Music topic :3,
692
- // stored agent=shumabit / cwd=$HOME vs the current
693
- // music-curation:music-curator / .../Music/rekordbox).
814
+ // and compare to the stored `sessions` row. On a normal cold spawn,
815
+ // resolveSessionForSpawn drops a stale row and returns
816
+ // existingSessionId:null. Runtime-switch preparation can defer that
817
+ // deletion so a rejected replacement cannot erase the dormant session.
694
818
  // model/effort are NOT compared — they apply live via setModel /
695
- // applyFlagSettings with no respawn. pm_backend is also NOT
696
- // compared (rc.32): both backends spawn the same pinned claude
697
- // binary against the same on-disk JSONL, so a backend flip
698
- // preserves context. See lib/db/sessions.js for full reasoning.
819
+ // applyFlagSettings with no respawn. Compatible inline-wrapper backend
820
+ // changes preserve context; the CLI Channels boundary remains
821
+ // invalidating. See lib/db/sessions.js for the compatibility rules.
699
822
  //
700
823
  // The drift check runs only at COLD spawn (no warm process). A warm
701
824
  // process already runs under its spawn-time config; getOrSpawn
702
825
  // returns it without using this context, so dropping its row here
703
826
  // would be premature — defer to the next cold spawn.
704
- const isColdSpawn = !pm || !pm.has(sessionKey) || pm.get(sessionKey)?.closed;
827
+ const liveProcess = pm?.get(sessionKey) ?? null;
828
+ const isColdSpawn = !processMatchesRuntime(
829
+ liveProcess,
830
+ 'claude',
831
+ promptBackend,
832
+ );
705
833
  let existingSessionId;
706
- if (isColdSpawn) {
834
+ if (strictResume) {
707
835
  const topicConfig = getTopicConfig(chatConfig, threadId || null);
708
836
  const resolved = {
709
837
  agent: topicConfig.agent || chatConfig.agent || null,
710
838
  cwd: topicConfig.cwd || chatConfig.cwd || null,
711
839
  backend: pickBackend({ config, chatId, threadId: threadId || null, pmDefault: 'sdk' }),
712
840
  };
713
- const r = resolveSessionForSpawn(db, sessionKey, resolved);
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) {
850
+ const topicConfig = getTopicConfig(chatConfig, threadId || null);
851
+ const resolved = {
852
+ agent: topicConfig.agent || chatConfig.agent || null,
853
+ cwd: topicConfig.cwd || chatConfig.cwd || null,
854
+ backend: pickBackend({ config, chatId, threadId: threadId || null, pmDefault: 'sdk' }),
855
+ };
856
+ const r = resolveSessionForSpawn(
857
+ db,
858
+ sessionKey,
859
+ resolved,
860
+ { mutateOnDrift: mutateSessionOnDrift },
861
+ );
714
862
  existingSessionId = r.existingSessionId;
715
863
  if (r.drift) {
716
864
  logEvent('session-config-drift', {
@@ -732,6 +880,11 @@ async function buildSpawnContext(sessionKey) {
732
880
  threadId: threadId || null,
733
881
  label: getSessionLabel(chatConfig, threadId),
734
882
  existingSessionId,
883
+ ...(strictResume ? {
884
+ resumePolicy: 'require-existing-session',
885
+ expectedSessionId: strictResume.expectedSessionId,
886
+ noWaitForCapacity: true,
887
+ } : {}),
735
888
  // File-send outbound cap inputs: localApi (backend ceiling) + the resolved
736
889
  // per-file override (topic → chat → bot → default) from the SAME resolver
737
890
  // the inbound filter and the send() choke point use, so CliProcess's
@@ -755,8 +908,13 @@ async function getOrSpawnForChat(sessionKey) {
755
908
  return pm.getOrSpawn(sessionKey, ctx);
756
909
  }
757
910
 
758
- async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched } = {}) {
759
- 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);
760
918
  if (!entry) throw new Error('No process for chat');
761
919
  const chatId = getChatIdFromKey(sessionKey);
762
920
  const chatConfig = config.chats[chatId];
@@ -789,6 +947,7 @@ async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched }
789
947
  maxTurnMs,
790
948
  maxTurnHardMs,
791
949
  context,
950
+ ...(expectedProcess ? { expectedProcess } : {}),
792
951
  });
793
952
  if (typeof onDispatched === 'function') onDispatched();
794
953
  return await turnP;
@@ -823,7 +982,13 @@ async function sendToProcess(sessionKey, prompt, context = {}, { onDispatched }
823
982
  // starts, which is the correct UX (and what the user already expects).
824
983
  const release = await stdinLock.acquire(sessionKey);
825
984
  try {
826
- 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
+ });
827
992
  // Phase 3 §4: pm.send synchronously kicks off the turn — the
828
993
  // process is now inFlight. Signal the committed-intent latch so
829
994
  // it can release; a concurrent handler will then correctly see
@@ -864,6 +1029,8 @@ let attemptAutoResume = null;
864
1029
  let errorReplyText = null;
865
1030
  let queueWarnThreshold = null;
866
1031
  let inFlightHandlers = null;
1032
+ let awaitHandlerSettlement = null;
1033
+ let trackHandlerTask = null;
867
1034
  let recoverCodexRequest = null;
868
1035
 
869
1036
  // AUTH_DISABLED (docs/AUTH_DISABLED_HANDLING_SPEC.md): dedupe/re-arm gate for
@@ -1262,7 +1429,12 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1262
1429
  await tg(bot, 'sendMessage', {
1263
1430
  chat_id: chatId, text: `Attachment(s) skipped: ${summary.slice(0, 300)}`,
1264
1431
  ...replyOpts(threadId),
1265
- }, { source: 'attachment-skipped', botName: BOT_NAME });
1432
+ }, {
1433
+ source: 'attachment-skipped',
1434
+ botName: BOT_NAME,
1435
+ sessionKey,
1436
+ sourceMsgId: msg.message_id,
1437
+ });
1266
1438
  } catch (err) {
1267
1439
  // Surface the failure: claude is about to reply as if the photo
1268
1440
  // was processed (because filterAttachments dropped it before
@@ -1305,6 +1477,8 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1305
1477
  const outMetaBase = {
1306
1478
  source: 'bot-reply-stream',
1307
1479
  botName: BOT_NAME,
1480
+ sessionKey,
1481
+ sourceMsgId: msg.message_id,
1308
1482
  model: chatConfig.model,
1309
1483
  effort: chatConfig.effort,
1310
1484
  ...(linkPreview === false ? { linkPreview: false } : {}),
@@ -1392,6 +1566,8 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1392
1566
  threadId,
1393
1567
  replyToMessageId: msg.message_id,
1394
1568
  botName: BOT_NAME,
1569
+ sessionKey,
1570
+ sourceMsgId: msg.message_id,
1395
1571
  logEvent: (kind, detail) => logEvent(kind, {
1396
1572
  chat_id: chatId, thread_id: threadId, bot: BOT_NAME, ...detail,
1397
1573
  }),
@@ -1498,6 +1674,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1498
1674
  phase: payload.phase,
1499
1675
  mediaContext,
1500
1676
  hadReplyAnchor: payload.hadReplyAnchor,
1677
+ meta: outMetaBase,
1501
1678
  });
1502
1679
  }
1503
1680
  const text = payload;
@@ -1512,7 +1689,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1512
1689
  chat_id: chatId,
1513
1690
  message_id: messageId,
1514
1691
  text: sanitizeLiveText(text),
1515
- }, { source: 'bot-reply-stream-edit', botName: BOT_NAME });
1692
+ }, { ...outMetaBase, source: 'bot-reply-stream-edit' });
1516
1693
  } catch (err) {
1517
1694
  // Stream-edit failures would otherwise be invisible — edits
1518
1695
  // don't insert a messages row by default (tg() does, but we
@@ -1537,7 +1714,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
1537
1714
  try {
1538
1715
  await tg(bot, 'deleteMessage', {
1539
1716
  chat_id: chatId, message_id: messageId,
1540
- }, { source: 'bot-reply-stream-discard', botName: BOT_NAME });
1717
+ }, { ...outMetaBase, source: 'bot-reply-stream-discard' });
1541
1718
  } catch (err) {
1542
1719
  console.error(`[${label}] discard preview failed: ${err.message}`);
1543
1720
  throw err;
@@ -2467,7 +2644,12 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
2467
2644
  chat_id: chatId,
2468
2645
  message_id: msg.message_id,
2469
2646
  reaction: [{ type: 'emoji', emoji }],
2470
- }, { ...outMeta, source: 'inline-reaction', reaction: emoji });
2647
+ }, {
2648
+ ...outMeta,
2649
+ deliveryClass: 'reply-bearing',
2650
+ source: 'inline-reaction',
2651
+ reaction: emoji,
2652
+ });
2471
2653
  } catch (err) {
2472
2654
  console.error(`[${label}] inline setMessageReaction(${emoji}) failed: ${err.message}`);
2473
2655
  }
@@ -2594,7 +2776,7 @@ async function handleMessage(sessionKey, chatId, msg, bot) {
2594
2776
  chat_id: chatId,
2595
2777
  message_id: msg.message_id,
2596
2778
  reaction: [{ type: 'emoji', emoji: parsed.reaction }],
2597
- }, outMeta).catch((err) => {
2779
+ }, { ...outMeta, deliveryClass: 'reply-bearing' }).catch((err) => {
2598
2780
  telegramDeliveryComplete = false;
2599
2781
  console.error(`[${label}] setMessageReaction failed: ${err.message}`);
2600
2782
  });
@@ -3336,7 +3518,8 @@ async function main() {
3336
3518
  );
3337
3519
  }
3338
3520
  }
3339
- tg = createSender(db, console, config);
3521
+ deliveryBarrier = createDeliveryBarrier();
3522
+ tg = createSender(db, console, config, deliveryBarrier);
3340
3523
  richMediaFileIdCache = createMediaFileIdCache();
3341
3524
  // ONE latch decision for both rich paths. They set the same flag, so a
3342
3525
  // counter owned by either would let that path latch on its first bare
@@ -3601,6 +3784,7 @@ async function main() {
3601
3784
 
3602
3785
  const sdkCallbacks = createSdkCallbacks({
3603
3786
  db, dbWrite, config, bot, botName: BOT_NAME, tg, logEvent, sessionFeedback,
3787
+ deliveryBarrier,
3604
3788
  classifyToolName, announce, shouldAnnounce, contextHintShown,
3605
3789
  extractAssistantText, getChatIdFromKey, getThreadIdFromKey,
3606
3790
  // F#23: enable parse/sanitize/sticker/react on the autonomous-wakeup path.
@@ -3971,6 +4155,27 @@ async function main() {
3971
4155
  config, db, dbWrite, pm, intentLock, getSessionKey,
3972
4156
  formatConfigInfoText, buildConfigKeyboard, saveConfig,
3973
4157
  resolveRuntimeView: resolveSessionRuntimeView,
4158
+ prepareRuntimeSelection: ({
4159
+ sessionKey,
4160
+ chatId,
4161
+ threadId,
4162
+ runtime,
4163
+ }) => {
4164
+ if (runtime !== 'codex' || !codexRuntimeController) {
4165
+ const error = new Error('Codex runtime preflight is unavailable');
4166
+ error.code = 'CODEX_RUNTIME_UNAVAILABLE';
4167
+ throw error;
4168
+ }
4169
+ return codexRuntimeController.resolveCandidateRuntimeView({
4170
+ sessionKey,
4171
+ chatId,
4172
+ threadId,
4173
+ });
4174
+ },
4175
+ discardRuntimeSelection: (sessionKey) => (
4176
+ codexRuntimeController?.discardCandidateRuntime(sessionKey) ?? false
4177
+ ),
4178
+ buildSpawnContext,
3974
4179
  botName: BOT_NAME, logger: console,
3975
4180
  });
3976
4181
  handleCodexReconciliationCallback = createHandleCodexReconciliationCallback({
@@ -3999,6 +4204,8 @@ async function main() {
3999
4204
  errorReplyText,
4000
4205
  queueWarnThreshold,
4001
4206
  inFlightHandlers,
4207
+ awaitSettlement: awaitHandlerSettlement,
4208
+ trackTask: trackHandlerTask,
4002
4209
  } = createDispatcher({
4003
4210
  config, db, dbWrite, tg, botName: BOT_NAME, logEvent,
4004
4211
  handleMessage, sendToProcess,
@@ -4210,18 +4417,91 @@ async function main() {
4210
4417
  const drainElapsed = Date.now() - drainStart;
4211
4418
  const remaining = countInFlight(inFlightHandlers);
4212
4419
 
4213
- // 3. A handled signal is normally deliberate, but a Linux supervisor can
4214
- // also stop the service after an OOM-killed child. Persist crash-like
4215
- // state when the process cgroup recorded an OOM kill; otherwise retain
4216
- // the clean-restart marker. Both paths mark in-flight work replayable in
4217
- // 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.
4218
4486
  if (db) {
4219
4487
  try {
4220
4488
  const res = persistShutdownDisposition({
4221
4489
  db,
4222
4490
  botName: BOT_NAME,
4223
4491
  observation: shutdownObservation,
4492
+ resumeIntents,
4493
+ forceCrashReason,
4224
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
+ }
4225
4505
  logEvent('shutdown-drain', {
4226
4506
  bot: BOT_NAME,
4227
4507
  in_flight: remaining,
@@ -4235,10 +4515,22 @@ async function main() {
4235
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`);
4236
4516
  } catch (err) {
4237
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
+ }
4238
4530
  }
4239
4531
  }
4240
4532
 
4241
- // 4. Remaining shutdown: approvals sweeper, IPC, resolve hook waiters,
4533
+ // 5. Remaining shutdown: approvals sweeper, IPC, resolve hook waiters,
4242
4534
  // kill pm subprocesses, close DB.
4243
4535
  if (approvalSweepTimer) clearInterval(approvalSweepTimer);
4244
4536
  authDisabledHeartbeat?.stop(); // null if we shut down before main() armed it
@@ -4250,7 +4542,7 @@ async function main() {
4250
4542
  // old `approvalWaiters` reference here would ReferenceError
4251
4543
  // mid-shutdown and prevent pm/DB cleanup.
4252
4544
  if (cancelAllWaiters) cancelAllWaiters('cancelled', 'polygram shutting down');
4253
- if (pm) await pm.shutdown().catch(() => {});
4545
+ if (pm && !pmRetired) await pm.shutdown().catch(() => {});
4254
4546
  if (db) {
4255
4547
  try { db.logEvent('polygram-stop'); db.raw.close(); } catch {}
4256
4548
  }
@@ -4314,24 +4606,194 @@ async function main() {
4314
4606
  // window discarded it; the agent's 7-hour Xero task was abandoned.
4315
4607
  // Auto-derive: 1.2 × max(chatConfig.maxTurn) across all chats,
4316
4608
  // floored at 3 min (legacy default), capped at 2 hours (sanity).
4609
+ const cleanRecoveryTasksBySession = new Map();
4317
4610
  try {
4318
4611
  const chatIds = Object.keys(config.chats);
4319
4612
  if (chatIds.length > 0) {
4320
4613
  const replayWindowMs = resolveReplayWindowMs(config);
4321
4614
 
4322
4615
  // 0.14: classify by RESTART INTENT. Read-and-clear the clean-shutdown
4323
- // marker FIRST, in its own try/catch — ANY error => treat as crash
4324
- // (recover), never skip-all (fail toward recovery). A deliberate restart
4325
- // skips re-answering stale messages and posts one visibility notice; a
4326
- // crash recovers everything (unchanged rc.57 behavior).
4327
- 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;
4328
4629
  try {
4329
- const maxAgeMs = 2 * (replayWindowMs || 3 * 60 * 1000);
4330
- 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
+ }
4331
4789
  } catch (err) {
4332
- console.error(`[replay] clean-shutdown marker read failed (-> crash recover): ${err.message}`);
4333
- 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;
4334
4795
  }
4796
+ const cleanShutdown = cleanRecovery.clean;
4335
4797
 
4336
4798
  const candidates = db.getReplayCandidates({ chatIds, ...(replayWindowMs && { olderThanMs: replayWindowMs }) });
4337
4799
 
@@ -4382,16 +4844,8 @@ async function main() {
4382
4844
  return msg;
4383
4845
  };
4384
4846
 
4385
- const replaySessionKey = (row) => {
4386
- const chatConfig = config.chats[row.chat_id];
4387
- if (!chatConfig) return null;
4388
- const threadId = row.thread_id == null
4389
- ? null
4390
- : String(row.thread_id);
4391
- return getSessionKey(row.chat_id, threadId, chatConfig);
4392
- };
4393
4847
  const getProviderRecovery = (row) => {
4394
- const sessionKey = replaySessionKey(row);
4848
+ const sessionKey = sourceSessionKey(row);
4395
4849
  if (!sessionKey) {
4396
4850
  return {
4397
4851
  provider: 'unknown',
@@ -4493,6 +4947,7 @@ async function main() {
4493
4947
  }
4494
4948
  }
4495
4949
  } catch (err) {
4950
+ if (err.code === 'CLEAN_RECOVERY_CLAIM_FAILED') throw err;
4496
4951
  console.error(`[replay] boot replay failed: ${err.message}`);
4497
4952
  }
4498
4953
 
@@ -4528,9 +4983,10 @@ async function main() {
4528
4983
  }
4529
4984
  let replayed = 0;
4530
4985
  let surfacedFallback = 0;
4531
- for (const o of orphansLatest.values()) {
4986
+ let deferred = 0;
4987
+ const recoverCompact = async (o) => {
4532
4988
  const chatCfg = config.chats[o.chat_id];
4533
- if (!chatCfg) continue;
4989
+ if (!chatCfg) return;
4534
4990
  const threadId = o.thread_id ? Number(o.thread_id) : null;
4535
4991
  const savedSessionId = getClaudeSessionId(db, o.session_key);
4536
4992
 
@@ -4559,7 +5015,7 @@ async function main() {
4559
5015
  user_id: o.user_id,
4560
5016
  });
4561
5017
  replayed += 1;
4562
- continue;
5018
+ return;
4563
5019
  } catch (err) {
4564
5020
  console.error(`[compact-replay] ${o.session_key}: ${err.message} — falling back to surface`);
4565
5021
  // fall through to surface fallback below
@@ -4588,9 +5044,24 @@ async function main() {
4588
5044
  } catch (err) {
4589
5045
  console.error(`[compact-orphan-surface] ${o.session_key}: ${err.message}`);
4590
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);
4591
5062
  }
4592
- if (replayed + surfacedFallback > 0) {
4593
- 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}`);
4594
5065
  }
4595
5066
  } catch (err) {
4596
5067
  console.error(`[compact-orphan-handler] failed: ${err.message}`);