instar 1.3.1668 → 1.3.1669

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.
Files changed (127) hide show
  1. package/dist/commands/init.d.ts.map +1 -1
  2. package/dist/commands/init.js +4 -19
  3. package/dist/commands/init.js.map +1 -1
  4. package/dist/commands/machine.d.ts.map +1 -1
  5. package/dist/commands/machine.js +32 -0
  6. package/dist/commands/machine.js.map +1 -1
  7. package/dist/commands/server.d.ts +4 -1
  8. package/dist/commands/server.d.ts.map +1 -1
  9. package/dist/commands/server.js +199 -30
  10. package/dist/commands/server.js.map +1 -1
  11. package/dist/core/ForwardedTopicContext.d.ts +6 -0
  12. package/dist/core/ForwardedTopicContext.d.ts.map +1 -1
  13. package/dist/core/ForwardedTopicContext.js +19 -0
  14. package/dist/core/ForwardedTopicContext.js.map +1 -1
  15. package/dist/core/MeshRpc.d.ts +4 -0
  16. package/dist/core/MeshRpc.d.ts.map +1 -1
  17. package/dist/core/MeshRpc.js +1 -0
  18. package/dist/core/MeshRpc.js.map +1 -1
  19. package/dist/core/PostUpdateMigrator.d.ts +18 -0
  20. package/dist/core/PostUpdateMigrator.d.ts.map +1 -1
  21. package/dist/core/PostUpdateMigrator.js +90 -1
  22. package/dist/core/PostUpdateMigrator.js.map +1 -1
  23. package/dist/core/SessionManager.d.ts +2 -0
  24. package/dist/core/SessionManager.d.ts.map +1 -1
  25. package/dist/core/SessionManager.js +36 -8
  26. package/dist/core/SessionManager.js.map +1 -1
  27. package/dist/core/SessionRouter.d.ts +3 -0
  28. package/dist/core/SessionRouter.d.ts.map +1 -1
  29. package/dist/core/SessionRouter.js +15 -0
  30. package/dist/core/SessionRouter.js.map +1 -1
  31. package/dist/core/StageAdvancer.d.ts +2 -0
  32. package/dist/core/StageAdvancer.d.ts.map +1 -1
  33. package/dist/core/StageAdvancer.js +19 -0
  34. package/dist/core/StageAdvancer.js.map +1 -1
  35. package/dist/core/WriteDomainRegistry.d.ts.map +1 -1
  36. package/dist/core/WriteDomainRegistry.js +15 -0
  37. package/dist/core/WriteDomainRegistry.js.map +1 -1
  38. package/dist/core/memorySessionPolicy.d.ts +4 -1
  39. package/dist/core/memorySessionPolicy.d.ts.map +1 -1
  40. package/dist/core/memorySessionPolicy.js +57 -15
  41. package/dist/core/memorySessionPolicy.js.map +1 -1
  42. package/dist/core/poolReadiness.d.ts +6 -3
  43. package/dist/core/poolReadiness.d.ts.map +1 -1
  44. package/dist/core/poolReadiness.js +6 -3
  45. package/dist/core/poolReadiness.js.map +1 -1
  46. package/dist/core/roleFollowRestart.d.ts +1 -1
  47. package/dist/core/roleFollowRestart.js +1 -1
  48. package/dist/core/sessionLiveProbe.d.ts +27 -0
  49. package/dist/core/sessionLiveProbe.d.ts.map +1 -0
  50. package/dist/core/sessionLiveProbe.js +31 -0
  51. package/dist/core/sessionLiveProbe.js.map +1 -0
  52. package/dist/core/sessionPoolPairActivation.d.ts +87 -0
  53. package/dist/core/sessionPoolPairActivation.d.ts.map +1 -0
  54. package/dist/core/sessionPoolPairActivation.js +229 -0
  55. package/dist/core/sessionPoolPairActivation.js.map +1 -0
  56. package/dist/core/sessionPoolPairGreen.d.ts +32 -0
  57. package/dist/core/sessionPoolPairGreen.d.ts.map +1 -0
  58. package/dist/core/sessionPoolPairGreen.js +51 -0
  59. package/dist/core/sessionPoolPairGreen.js.map +1 -0
  60. package/dist/core/telegramPairHandover.d.ts +27 -0
  61. package/dist/core/telegramPairHandover.d.ts.map +1 -0
  62. package/dist/core/telegramPairHandover.js +42 -0
  63. package/dist/core/telegramPairHandover.js.map +1 -0
  64. package/dist/core/types.d.ts +6 -0
  65. package/dist/core/types.d.ts.map +1 -1
  66. package/dist/core/types.js.map +1 -1
  67. package/dist/data/standards-guard-index.json +1 -1
  68. package/dist/data/standards-guard-index.meta.json +2 -2
  69. package/dist/data/standards-registry.meta.json +1 -1
  70. package/dist/messaging/MessageProcessingLedger.d.ts +6 -0
  71. package/dist/messaging/MessageProcessingLedger.d.ts.map +1 -1
  72. package/dist/messaging/MessageProcessingLedger.js +9 -0
  73. package/dist/messaging/MessageProcessingLedger.js.map +1 -1
  74. package/dist/messaging/slack/SlackAdapter.d.ts.map +1 -1
  75. package/dist/messaging/slack/SlackAdapter.js +4 -0
  76. package/dist/messaging/slack/SlackAdapter.js.map +1 -1
  77. package/dist/messaging/stuckMessageRecovery.d.ts +2 -0
  78. package/dist/messaging/stuckMessageRecovery.d.ts.map +1 -1
  79. package/dist/messaging/stuckMessageRecovery.js +19 -12
  80. package/dist/messaging/stuckMessageRecovery.js.map +1 -1
  81. package/dist/native/InstarWedgeCanary.app/Contents/MacOS/InstarWedgeCanary +0 -0
  82. package/dist/native/instar-desktop-host +0 -0
  83. package/dist/scaffold/templates.d.ts.map +1 -1
  84. package/dist/scaffold/templates.js +2 -1
  85. package/dist/scaffold/templates.js.map +1 -1
  86. package/dist/scheduler/JobScheduler.d.ts.map +1 -1
  87. package/dist/scheduler/JobScheduler.js +15 -7
  88. package/dist/scheduler/JobScheduler.js.map +1 -1
  89. package/dist/server/AgentServer.d.ts.map +1 -1
  90. package/dist/server/AgentServer.js +7 -0
  91. package/dist/server/AgentServer.js.map +1 -1
  92. package/dist/server/CapabilityIndex.d.ts.map +1 -1
  93. package/dist/server/CapabilityIndex.js +10 -0
  94. package/dist/server/CapabilityIndex.js.map +1 -1
  95. package/dist/server/machineRoutes.d.ts +5 -0
  96. package/dist/server/machineRoutes.d.ts.map +1 -1
  97. package/dist/server/machineRoutes.js +26 -0
  98. package/dist/server/machineRoutes.js.map +1 -1
  99. package/dist/server/mobilePairingRoutes.d.ts.map +1 -1
  100. package/dist/server/mobilePairingRoutes.js +4 -0
  101. package/dist/server/mobilePairingRoutes.js.map +1 -1
  102. package/dist/server/mobilePermissionsRoutes.d.ts +21 -2
  103. package/dist/server/mobilePermissionsRoutes.d.ts.map +1 -1
  104. package/dist/server/mobilePermissionsRoutes.js +86 -7
  105. package/dist/server/mobilePermissionsRoutes.js.map +1 -1
  106. package/dist/server/mobileSecretsRoutes.d.ts +34 -1
  107. package/dist/server/mobileSecretsRoutes.d.ts.map +1 -1
  108. package/dist/server/mobileSecretsRoutes.js +30 -2
  109. package/dist/server/mobileSecretsRoutes.js.map +1 -1
  110. package/dist/server/routes.d.ts.map +1 -1
  111. package/dist/server/routes.js +54 -3
  112. package/dist/server/routes.js.map +1 -1
  113. package/package.json +1 -1
  114. package/src/data/builtin-manifest.json +95 -95
  115. package/src/data/standards-guard-index.json +1 -1
  116. package/src/data/standards-guard-index.meta.json +2 -2
  117. package/src/data/standards-registry.meta.json +1 -1
  118. package/src/scaffold/templates.ts +2 -1
  119. package/upgrades/1.3.1669.md +60 -0
  120. package/upgrades/side-effects/failover-reanswer.md +82 -0
  121. package/upgrades/side-effects/lease-scheduler-role-continuity-release-fragment.md +40 -0
  122. package/upgrades/side-effects/lease-scheduler-role-continuity.md +43 -0
  123. package/upgrades/side-effects/legacy-default-job-reconciliation-release-fragment.md +40 -0
  124. package/upgrades/side-effects/legacy-default-job-reconciliation.md +47 -0
  125. package/upgrades/side-effects/memory-privacy-inbound-files.md +55 -0
  126. package/upgrades/side-effects/mobile-native-secrets-writes.md +19 -0
  127. package/upgrades/side-effects/session-pool-activation-at-pairing.md +53 -0
@@ -113,6 +113,8 @@ import { gapBEligibleForTopic, recentUserMessageFromHistory, recentUserMessageAt
113
113
  import { TopicProfileTransferCarrier, createTopicProfilePullHandler } from '../core/TopicProfileTransferCarrier.js';
114
114
  import { resolveFrameworkBinaryPath, computeGrokInteractiveOptIn } from '../core/frameworkSessionLaunch.js';
115
115
  import { runResumeHeartbeatTick } from '../core/ResumeHeartbeat.js';
116
+ import { activatedAtPairing, readPairDecision } from '../core/sessionPoolPairActivation.js';
117
+ import { liveCopyInList, probeLiveCopyOnPeer } from '../core/sessionLiveProbe.js';
116
118
  import { closeAllSqlite } from '../core/SqliteRegistry.js';
117
119
  import { DP_SUBSCRIPTION_RELOGIN_ACTION, DP_TELEGRAM_ORIGIN_RECOVERY } from '../data/provenanceCoverage.js';
118
120
  import { buildBoundedContext, buildStructuredSha256Identity } from '../core/JudgmentProvenanceLog.js';
@@ -187,7 +189,8 @@ import { isRemotelyHandled } from '../core/SessionRouter.js';
187
189
  import { confirmLocalPlacementAfterDelivery } from '../core/SessionPoolLocalClaim.js';
188
190
  import { decideLeaseGatedSpawn, sharedG3SoakLedger, applyBindingCleanupOnKill } from '../core/leaseGatedSpawn.js';
189
191
  import { isSlackSessionKey, reconstructSlackMessage } from '../core/SlackForwardBridge.js';
190
- import { formatForwardedTopicContext } from '../core/ForwardedTopicContext.js';
192
+ import { formatForwardedTopicContext, crossMachineReplyEvidence, INCOMPLETE_TOPIC_HISTORY_WARNING } from '../core/ForwardedTopicContext.js';
193
+ import { CoherenceJournalReader } from '../core/CoherenceJournalReader.js';
191
194
  import { createTopicHistoryPullHandler, pullMovedTopicHistory, TOPIC_HISTORY_PULL_MAX_MESSAGES, TOPIC_HISTORY_PULL_TIMEOUT_MS } from '../core/topicHistoryPull.js';
192
195
  import { resolveAdvertisedMeshUrl, advertiseSelfMeshUrl, detectTailscaleIp, pickPrimaryLanIp, computeSelfMeshEndpoints, advertiseSelfMeshEndpoints, resolveMeshBindHost } from '../core/MeshUrlAdvertiser.js';
193
196
  import { PeerEndpointResolver } from '../core/PeerEndpointResolver.js';
@@ -607,6 +610,12 @@ let _clearContextExhaustionFreshRequired = null;
607
610
  // is byte-identical to today's always-local dispatch. Set once in startServer().
608
611
  let _sessionRouter = null;
609
612
  let _sessionPoolStage = () => 'dark';
613
+ /** A pool started at pairing, stepped down to dark, with this conversation on a peer: the
614
+ * next message still goes there (session-pool-activation-at-pairing, Change 7). */
615
+ /** The signed duplicate probe, both sides (set where the mesh client and the check are built). */
616
+ let _signedLiveCopyProbe = null;
617
+ let _serveLiveCopyProbe = null;
618
+ let _routesToPeerAtDark = () => false;
610
619
  // Self-placement deliberately falls through from SessionRouter into this file's
611
620
  // established local inject/spawn tail. Confirming inside SessionRouter would be
612
621
  // premature because handleLocally is only that fall-through marker. This callback
@@ -1404,6 +1413,7 @@ spawnOpts) {
1404
1413
  const bootstrapFilename = `bootstrap-${topicId}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}.txt`;
1405
1414
  const bootstrapFilepath = path.join(tmpDir, bootstrapFilename);
1406
1415
  fs.writeFileSync(bootstrapFilepath, bootstrapMessage);
1416
+ sessionManager.registerMemoryInboundFile(`telegram:${topicId}`, bootstrapFilepath);
1407
1417
  console.log(`[spawnSessionForTopic] Bootstrap message too large (${bootstrapMessage.length} chars), wrote to ${bootstrapFilepath}`);
1408
1418
  bootstrapMessage = `[IMPORTANT: Read ${bootstrapFilepath} — it contains your full session context, conversation history, and the user's latest message. You MUST read this file before responding.]`;
1409
1419
  }
@@ -2353,7 +2363,7 @@ getAttentionTopicId,
2353
2363
  // message + recent conversation (HubIntentClassifier), NOT a regex — the
2354
2364
  // regex SWALLOWED the message before the agent saw it, so a misread ATE it.
2355
2365
  // Resolved at the callsite where config + sharedIntelligence are in scope.
2356
- getHubClassifierDeps) {
2366
+ getHubClassifierDeps, poolHistoryAccess) {
2357
2367
  // Guard: tracks which topic IDs have a spawn in progress.
2358
2368
  // Prevents duplicate concurrent spawns for the same topic when messages
2359
2369
  // arrive faster than the async spawn completes.
@@ -2398,6 +2408,10 @@ getHubClassifierDeps) {
2398
2408
  return;
2399
2409
  }
2400
2410
  }
2411
+ for (const file of [msg.metadata?.documentPath, msg.metadata?.photoPath]) {
2412
+ if (typeof file === 'string')
2413
+ sessionManager.registerMemoryInboundFile(`telegram:${topicId}`, file);
2414
+ }
2401
2415
  // Agent-signed inbound (ASP). A message an agent SIGNED and delivered through
2402
2416
  // a human's account is labelled as agent traffic for the receiving session and
2403
2417
  // NEVER binds that human as the topic's operator. The verdict is the
@@ -2671,7 +2685,8 @@ getHubClassifierDeps) {
2671
2685
  // CONSUMES it instead of re-resolving — the admission decision and the
2672
2686
  // routing decision cannot disagree mid-dispatch.
2673
2687
  let _admissionVerdict;
2674
- if (_sessionRouter && _sessionPoolStage() !== 'dark') {
2688
+ const _routingOnly = _routesToPeerAtDark(String(topicId));
2689
+ if (_sessionRouter && (_sessionPoolStage() !== 'dark' || _routingOnly)) {
2675
2690
  // Ordering gate (Durable Inbound Message Queue §2.3): a live message for
2676
2691
  // a session with queued custody enqueues BEHIND the existing entries —
2677
2692
  // injecting it now would deliver out of order. Gated on a live engine.
@@ -2700,6 +2715,7 @@ getHubClassifierDeps) {
2700
2715
  sessionKey: String(topicId),
2701
2716
  messageId: String(msg.id),
2702
2717
  payload: text,
2718
+ ...(_routingOnly ? { routingOnly: true } : {}),
2703
2719
  topicMetadata: _pinPlacementMetadata ? _pinPlacementMetadata(String(topicId)) : _topicPinStore?.asTopicMetadata(String(topicId)), // U4.1 §2D: local pin ⊕ fold winner by HLC
2704
2720
  // §2.2: sender identity captured at ingress, persisted with custody.
2705
2721
  senderEnvelope: { userId: telegramUserId || undefined, firstName: pipeline.sender.firstName, ...mobileEnvelope(msg) },
@@ -3034,7 +3050,29 @@ getHubClassifierDeps) {
3034
3050
  if (!spawnName)
3035
3051
  spawnName = `topic-${topicId}`;
3036
3052
  // Use the shared spawn helper that includes topic history + user context
3037
- spawnSessionForTopic(sessionManager, telegram, spawnName, topicId, text, topicMemory, resolvedUser ?? undefined).then((newSessionName) => {
3053
+ (async () => {
3054
+ let movedContext;
3055
+ if (poolHistoryAccess && _sessionPoolStage() !== 'dark') {
3056
+ try {
3057
+ const r = await fetch(`http://127.0.0.1:${poolHistoryAccess.port}/telegram/topics/${topicId}/messages?limit=100&scope=pool`, {
3058
+ headers: { Authorization: `Bearer ${poolHistoryAccess.authToken}` }, signal: AbortSignal.timeout(8_000),
3059
+ });
3060
+ if (r.ok) {
3061
+ const j = await r.json();
3062
+ const history = Array.isArray(j.messages) ? j.messages : [];
3063
+ movedContext = `${INCOMPLETE_TOPIC_HISTORY_WARNING}\n\n${formatForwardedTopicContext(history, spawnName)}`;
3064
+ if (j.pool?.failed?.length || history.length >= 100)
3065
+ movedContext = `${INCOMPLETE_TOPIC_HISTORY_WARNING}\n\n${movedContext ?? ''}`;
3066
+ }
3067
+ else
3068
+ movedContext = INCOMPLETE_TOPIC_HISTORY_WARNING;
3069
+ }
3070
+ catch {
3071
+ movedContext = INCOMPLETE_TOPIC_HISTORY_WARNING;
3072
+ }
3073
+ }
3074
+ return spawnSessionForTopic(sessionManager, telegram, spawnName, topicId, text, topicMemory, resolvedUser ?? undefined, movedContext);
3075
+ })().then((newSessionName) => {
3038
3076
  telegram.registerTopicSession(topicId, newSessionName, spawnName);
3039
3077
  confirmLocalSessionPoolClaim();
3040
3078
  sendDeterministicTelegramNotice(telegram, 'session-lifecycle', topicId, `Session starting up — reading your message now. One moment.`).catch(() => {
@@ -3116,7 +3154,8 @@ getHubClassifierDeps) {
3116
3154
  // enabled, but cross-machine routing is the failover machinery a red E2E
3117
3155
  // demoted. At dark the drain delivers on THIS machine only, exactly like the
3118
3156
  // live path, which skips the router at dark.
3119
- const poolDark = _sessionPoolStage() === 'dark';
3157
+ const drainRoutingOnly = _routesToPeerAtDark(dmsg.sessionKey);
3158
+ const poolDark = _sessionPoolStage() === 'dark' && !drainRoutingOnly;
3120
3159
  if (!poolDark && !_sessionRouter)
3121
3160
  return { kind: 'un-routable', reason: 'router-not-constructed' };
3122
3161
  // Routing first — ownership may have moved while the entry waited.
@@ -3124,6 +3163,7 @@ getHubClassifierDeps) {
3124
3163
  sessionKey: dmsg.sessionKey,
3125
3164
  messageId: dmsg.messageId,
3126
3165
  payload: dmsg.payload,
3166
+ ...(drainRoutingOnly ? { routingOnly: true } : {}),
3127
3167
  topicMetadata: dmsg.topicMetadata
3128
3168
  ?? (_pinPlacementMetadata ? _pinPlacementMetadata(dmsg.sessionKey) : _topicPinStore?.asTopicMetadata(dmsg.sessionKey)), // U4.1 §2D
3129
3169
  senderEnvelope: dmsg.senderEnvelope,
@@ -6416,6 +6456,7 @@ export async function startServer(options) {
6416
6456
  catch { /* @silent-fallback-ok — the stamp only arms the fail-open straggler backstop */ }
6417
6457
  const sessionManagerConfig = {
6418
6458
  ...config.sessions,
6459
+ stateDir: config.stateDir,
6419
6460
  // §7: mint the per-session bind token into INSTAR_BIND_TOKEN at spawn.
6420
6461
  mintBindToken: (sessionName, bootstrapConversationIds) => conversationBindAuth.mint(sessionName, bootstrapConversationIds),
6421
6462
  ...(codexThreadlineMcp ? { codexThreadlineMcp } : {}),
@@ -7874,11 +7915,11 @@ export async function startServer(options) {
7874
7915
  const nowAwake = coordinator.isAwake;
7875
7916
  if (nowAwake === bootAwake)
7876
7917
  return;
7877
- const lease = config.multiMachine?.leaseSelfHeal;
7878
- const peer = Object.entries(coordinator.managers.identityManager.loadRegistry().machines ?? {})
7879
- .some(([id, entry]) => id !== coordinator.identity?.machineId && !entry.revokedAt);
7880
7918
  const decision = decideRoleRestart({
7881
- applies: !!lease?.soloCaptainHold?.enabled && !lease?.preferredAwakeMachineId && peer,
7919
+ // Every scheduler-enabled lease holder needs the awake boot path. The
7920
+ // optional self-heal policy and preferred-machine hint do not change
7921
+ // which machine currently owns the lease.
7922
+ applies: config.scheduler.enabled,
7882
7923
  bootAwake, nowAwake, lastRestartAtMs: readLastRoleRestart(config.stateDir), nowMs: Date.now(),
7883
7924
  });
7884
7925
  if (decision === 'restart') {
@@ -7891,13 +7932,13 @@ export async function startServer(options) {
7891
7932
  }
7892
7933
  };
7893
7934
  coordinator.on('roleChange', (_from, to) => {
7894
- const lease = config.multiMachine?.leaseSelfHeal;
7895
- if (lease?.soloCaptainHold?.enabled && !lease?.preferredAwakeMachineId) {
7896
- if (to === 'awake')
7897
- scheduler?.resume();
7898
- else
7899
- scheduler?.pause();
7900
- }
7935
+ // A demoted holder stops jobs immediately, even while the planned
7936
+ // restart is throttled or pending. A promoted standby restarts through
7937
+ // the normal boot path, which constructs its missing scheduler.
7938
+ if (to === 'awake')
7939
+ scheduler?.resume();
7940
+ else
7941
+ scheduler?.pause();
7901
7942
  reconcileRole();
7902
7943
  });
7903
7944
  // A change inside the restart window, or a failed request, is retried until the role matches.
@@ -8326,7 +8367,7 @@ export async function startServer(options) {
8326
8367
  const userManagerSendOnly = new UserManager(config.stateDir, config.users);
8327
8368
  attachUserReplication(userManagerSendOnly); // WS2.6 send-side (dark by default)
8328
8369
  _fixDeps = { state, liveConfig, sessionManager, telegram, config };
8329
- wireTelegramRouting(telegram, sessionManager, quotaTracker, topicMemory, userManagerSendOnly, (topicId, text) => handleFixCommand(topicId, text, _fixDeps), () => (collaborationSurfacer && conversationStore && telegram) ? { collaborationSurfacer, conversationStore, commitmentTracker, telegram, brief: briefDeps } : null, () => _agentServerRef?.getTopicOperatorStore() ?? null, () => state.get('agent-attention-topic'), () => resolveHubClassifierDeps(config, _sharedIntelligence));
8370
+ wireTelegramRouting(telegram, sessionManager, quotaTracker, topicMemory, userManagerSendOnly, (topicId, text) => handleFixCommand(topicId, text, _fixDeps), () => (collaborationSurfacer && conversationStore && telegram) ? { collaborationSurfacer, conversationStore, commitmentTracker, telegram, brief: briefDeps } : null, () => _agentServerRef?.getTopicOperatorStore() ?? null, () => state.get('agent-attention-topic'), () => resolveHubClassifierDeps(config, _sharedIntelligence), { port: config.port, authToken: config.authToken ?? '' });
8330
8371
  wireTelegramCallbacks(telegram, sessionManager, state, quotaTracker, undefined, config.sessions.claudePath, topicMemory);
8331
8372
  console.log(pc.green(' Telegram routing + command callbacks wired (send-only)'));
8332
8373
  }
@@ -8440,7 +8481,7 @@ export async function startServer(options) {
8440
8481
  config,
8441
8482
  };
8442
8483
  // Wire up topic → session routing and session management callbacks
8443
- wireTelegramRouting(telegram, sessionManager, quotaTracker, topicMemory, userManager, (topicId, text) => handleFixCommand(topicId, text, _fixDeps), () => (collaborationSurfacer && conversationStore && telegram) ? { collaborationSurfacer, conversationStore, commitmentTracker, telegram, brief: briefDeps } : null, () => _agentServerRef?.getTopicOperatorStore() ?? null, () => state.get('agent-attention-topic'), () => resolveHubClassifierDeps(config, _sharedIntelligence));
8484
+ wireTelegramRouting(telegram, sessionManager, quotaTracker, topicMemory, userManager, (topicId, text) => handleFixCommand(topicId, text, _fixDeps), () => (collaborationSurfacer && conversationStore && telegram) ? { collaborationSurfacer, conversationStore, commitmentTracker, telegram, brief: briefDeps } : null, () => _agentServerRef?.getTopicOperatorStore() ?? null, () => state.get('agent-attention-topic'), () => resolveHubClassifierDeps(config, _sharedIntelligence), { port: config.port, authToken: config.authToken ?? '' });
8444
8485
  wireTelegramCallbacks(telegram, sessionManager, state, quotaTracker, accountSwitcher, config.sessions.claudePath, topicMemory);
8445
8486
  // Wire up unknown-user handling (Multi-User Setup Wizard Phase 4.5)
8446
8487
  telegram.onGetRegistrationPolicy = () => ({
@@ -9669,6 +9710,10 @@ export async function startServer(options) {
9669
9710
  }
9670
9711
  }
9671
9712
  // Sanitize sender name at injection boundary (prevents injection attacks)
9713
+ for (const file of (Array.isArray(message.metadata?.inboundFilePaths) ? message.metadata.inboundFilePaths : [])) {
9714
+ if (typeof file === 'string')
9715
+ sessionManager.registerMemoryInboundFile(`slack:${channelId}`, file);
9716
+ }
9672
9717
  const safeSenderName = senderName
9673
9718
  ? senderName.replace(/[\x00-\x1f\x7f]/g, '').replace(/\s+/g, ' ').replace(/["\[\]]/g, '').trim().slice(0, 64) || 'User'
9674
9719
  : undefined;
@@ -9741,6 +9786,7 @@ export async function startServer(options) {
9741
9786
  // Also write to file as backup reference
9742
9787
  const ctxPath = path.join(tmpDir, `ctx-${channelId}-${Date.now()}.txt`);
9743
9788
  fs.writeFileSync(ctxPath, contextData);
9789
+ sessionManager.registerMemoryInboundFile(`slack:${channelId}`, ctxPath);
9744
9790
  // Transform [image:path] and [document:path] tags into explicit read instructions
9745
9791
  let transformedContent = message.content.replace(/\[image:([^\]]+)\]/g, (_, imagePath) => {
9746
9792
  if (imagePath === 'download-failed') {
@@ -9764,6 +9810,7 @@ export async function startServer(options) {
9764
9810
  const msgFilePath = path.join(tmpDir, `msg-${channelId}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}.txt`);
9765
9811
  const fullContent = `${fullMessage}\n\n${contextData}`;
9766
9812
  fs.writeFileSync(msgFilePath, fullContent);
9813
+ sessionManager.registerMemoryInboundFile(`slack:${channelId}`, msgFilePath);
9767
9814
  bootstrapMessage = `${prefix} [Long message saved to ${msgFilePath} — read it to see the full message and thread history]`;
9768
9815
  }
9769
9816
  else {
@@ -10022,12 +10069,14 @@ export async function startServer(options) {
10022
10069
  // SpawnAdmission TOCTOU guard (ownership-gated-spawn §3.1 item 2):
10023
10070
  // stash the router verdict so the dispatch's seam consumes it.
10024
10071
  let _slackAdmissionVerdict;
10025
- if (_sessionRouter && _sessionPoolStage() !== 'dark') {
10072
+ const _slackRoutingOnly = _routesToPeerAtDark(routingKey);
10073
+ if (_sessionRouter && (_sessionPoolStage() !== 'dark' || _slackRoutingOnly)) {
10026
10074
  try {
10027
10075
  const outcome = await _sessionRouter.route({
10028
10076
  sessionKey: routingKey,
10029
10077
  messageId: String(message.id),
10030
10078
  payload: message.content,
10079
+ ...(_slackRoutingOnly ? { routingOnly: true } : {}),
10031
10080
  topicMetadata: _pinPlacementMetadata ? _pinPlacementMetadata(routingKey) : _topicPinStore?.asTopicMetadata(routingKey), // U4.1 §2D: local pin ⊕ fold winner by HLC
10032
10081
  senderEnvelope: {
10033
10082
  userId: message.metadata?.slackUserId || undefined,
@@ -23697,6 +23746,10 @@ export async function startServer(options) {
23697
23746
  .map(([id]) => id),
23698
23747
  capacity: (id) => machinePoolRegistry.getCapacityForReport(id),
23699
23748
  ownChannels: () => selfServesChannels(),
23749
+ pairingDeclined: () => {
23750
+ const decision = readPairDecision(config.stateDir);
23751
+ return decision && !decision.activate ? decision.reason : null;
23752
+ },
23700
23753
  durableOwnership: () => durableOwnershipMod.shouldActivateDurableOwnership(config, resolveDevAgentGate),
23701
23754
  duplicateCheck: () => _duplicateReconciler?.status() ?? null,
23702
23755
  takeover: () => _takeoverReadiness,
@@ -25918,6 +25971,15 @@ export async function startServer(options) {
25918
25971
  // every follow-up — the moved conversation never advances. So: if a live
25919
25972
  // session exists, inject the text into it (with the [telegram:N …] prefix the
25920
25973
  // session expects) and return; only spawn when there is none.
25974
+ // The receiver owns the reply, so claim the original inbound key here.
25975
+ // Its outbound commit then broadcasts a marker back to the router's
25976
+ // ledger, which otherwise stays processing forever.
25977
+ if (messageLedger && currentInboundByTopic && /^\d+$/.test(cmd.messageId)) {
25978
+ const key = dedupeKeyFor('telegram', topicId, cmd.messageId);
25979
+ if (messageLedger.claimForwardedInbound(key, String(topicId), text ?? '', 0)) {
25980
+ currentInboundByTopic.set(String(topicId), key);
25981
+ }
25982
+ }
25921
25983
  const existing = tg.getSessionForTopic(topicId);
25922
25984
  if (existing && sessionManager.isSessionAlive(existing)) {
25923
25985
  if (text) {
@@ -25959,18 +26021,21 @@ export async function startServer(options) {
25959
26021
  const url = _resolveRouterUrl?.() ?? null;
25960
26022
  if (!url)
25961
26023
  return null;
25962
- const resp = await fetch(`${url}/telegram/topics/${topic}/messages?limit=${TOPIC_HISTORY_PULL_MAX_MESSAGES}`, {
26024
+ const resp = await fetch(`${url}/telegram/topics/${topic}/messages?limit=${TOPIC_HISTORY_PULL_MAX_MESSAGES}&scope=pool`, {
25963
26025
  headers: { 'Authorization': `Bearer ${config.authToken}` },
25964
26026
  signal: AbortSignal.timeout(TOPIC_HISTORY_PULL_TIMEOUT_MS),
25965
26027
  });
25966
26028
  if (!resp.ok)
25967
26029
  return null;
25968
26030
  const j = (await resp.json());
26031
+ if (j.pool?.failed?.length)
26032
+ console.warn(` [session-pool] pool history incomplete for topic ${topic}`);
25969
26033
  return Array.isArray(j.messages) ? j.messages : null;
25970
26034
  },
25971
26035
  log: (m) => console.warn(` [session-pool] ${m}`),
25972
26036
  }, topicId);
25973
- const movedContext = formatForwardedTopicContext(history.messages, tg.getTopicName?.(topicId) ?? undefined) || undefined;
26037
+ // A bounded pull cannot prove that no older messages lived on a prior machine.
26038
+ const movedContext = `${INCOMPLETE_TOPIC_HISTORY_WARNING}\n\n${formatForwardedTopicContext(history.messages, tg.getTopicName?.(topicId) ?? undefined)}`;
25974
26039
  return spawnSessionForTopic(sessionManager, tg, spawnName, topicId, text, undefined, undefined, movedContext);
25975
26040
  })()
25976
26041
  .then((name) => {
@@ -26412,6 +26477,12 @@ export async function startServer(options) {
26412
26477
  'topic-history-pull': (cmd) => createTopicHistoryPullHandler({
26413
26478
  readHistory: telegram ? (topic, limit) => telegram.getTopicHistory(topic, limit) : null,
26414
26479
  })(cmd),
26480
+ 'session-live-probe': async (cmd) => {
26481
+ const { platform, platformId } = cmd;
26482
+ if (typeof platform !== 'string' || typeof platformId !== 'string')
26483
+ return { ok: false, reason: 'malformed' };
26484
+ return _serveLiveCopyProbe ? _serveLiveCopyProbe(platform, platformId) : { ok: false, reason: 'no-duplicate-check' };
26485
+ },
26415
26486
  'topic-profile-pull': (cmd) => {
26416
26487
  if (!_topicProfileStore)
26417
26488
  return { ok: false, reason: 'topic-profile disabled' };
@@ -27008,6 +27079,7 @@ export async function startServer(options) {
27008
27079
  const h = coordinator.getSyncStatus().leaseHolder;
27009
27080
  return h && h !== meshSelfId ? peerUrl(h) : null;
27010
27081
  };
27082
+ _signedLiveCopyProbe = (machineId, url, platform, platformId) => meshClient.send({ machineId, url }, { type: 'session-live-probe', platform, platformId }, 0, { timeoutMs: 5_000 });
27011
27083
  _pullRouterHistory = async (topic) => {
27012
27084
  const h = coordinator.getSyncStatus().leaseHolder;
27013
27085
  const url = h && h !== meshSelfId ? peerUrl(h) : null;
@@ -28399,6 +28471,14 @@ export async function startServer(options) {
28399
28471
  throw new Error(`HTTP ${r.status}`);
28400
28472
  return r.json();
28401
28473
  };
28474
+ _serveLiveCopyProbe = async (platform, platformId) => {
28475
+ try {
28476
+ return { ok: true, live: liveCopyInList(await fetchJson(`${selfBase}/sessions`, 5_000), platform, platformId) };
28477
+ }
28478
+ catch (err) {
28479
+ return { ok: false, reason: err instanceof Error ? err.message : String(err) };
28480
+ }
28481
+ };
28402
28482
  const dupCfgResolved = {
28403
28483
  enabled: resolveDevAgentGate(dupCfgRaw.enabled, config),
28404
28484
  dryRun: dupCfgRaw.dryRun !== false,
@@ -28456,14 +28536,15 @@ export async function startServer(options) {
28456
28536
  const sep = key.indexOf(':');
28457
28537
  const platform = key.slice(0, sep);
28458
28538
  const pid = key.slice(sep + 1);
28459
- const base = machineId === _meshSelfId
28460
- ? selfBase
28461
- : (_resolvePeerUrls?.() ?? []).find((p) => p.machineId === machineId)?.url;
28539
+ const onSelf = machineId === _meshSelfId;
28540
+ const base = onSelf ? selfBase : (_resolvePeerUrls?.() ?? []).find((p) => p.machineId === machineId)?.url;
28462
28541
  if (!base)
28463
28542
  return { ok: false, live: false };
28464
- const list = (await fetchJson(`${base}/sessions`, 5_000));
28465
- const live = (Array.isArray(list) ? list : []).some((s) => String(s.platform) === platform && String(s.platformId) === pid);
28466
- return { ok: true, live };
28543
+ const bearer = async () => liveCopyInList(await fetchJson(`${base}/sessions`, 5_000), platform, pid);
28544
+ return await probeLiveCopyOnPeer({
28545
+ signed: async () => (onSelf || !_signedLiveCopyProbe ? null : _signedLiveCopyProbe(machineId, base, platform, pid)),
28546
+ bearer,
28547
+ });
28467
28548
  }
28468
28549
  catch {
28469
28550
  // @silent-fallback-ok: ok:false is the DEFERRAL signal — the
@@ -29095,6 +29176,20 @@ export async function startServer(options) {
29095
29176
  return 'dark';
29096
29177
  }
29097
29178
  };
29179
+ _routesToPeerAtDark = (sessionKey) => {
29180
+ try {
29181
+ const live = liveConfig.get('multiMachine.sessionPool', config.multiMachine?.sessionPool ?? {});
29182
+ if (live.enabled !== true || _sessionPoolStage() !== 'dark')
29183
+ return false;
29184
+ if (!activatedAtPairing({ multiMachine: { sessionPool: live } }))
29185
+ return false;
29186
+ const own = _ownershipReadForDrain?.(sessionKey);
29187
+ return !!own && own.status === 'active' && own.ownerMachineId !== _meshSelfId;
29188
+ }
29189
+ catch {
29190
+ return false;
29191
+ } // @silent-fallback-ok — an unreadable record falls back to local delivery, as at any dark stage
29192
+ };
29098
29193
  console.log(pc.green(' SessionRouter wired (L4) — inert until rollout stage advances past dark'));
29099
29194
  // bug #7: tokenless-standby outbound Telegram relay. TelegramAdapter.sendToTopic
29100
29195
  // only invokes this when the adapter has NO bot token (a pool standby serving a
@@ -29562,6 +29657,12 @@ export async function startServer(options) {
29562
29657
  // Splitting these previously let the runner stamp every green as stage 0
29563
29658
  // while StageAdvancer was already waiting for a green at stage 2.
29564
29659
  const readSessionPoolStage = () => liveConfig.get('multiMachine.sessionPool.stage', 'dark');
29660
+ // A pool started at pairing has its own green and expiry. Every other pool keeps today's rules.
29661
+ const pairStartedPool = activatedAtPairing(config);
29662
+ const pairGreenMod = await import('../core/sessionPoolPairGreen.js');
29663
+ const pairPeerExists = () => (coordinator?.managers.identityManager.getActiveMachines() ?? [])
29664
+ .some((m) => m.machineId !== _meshSelfId);
29665
+ let pairGreenProducer = null;
29565
29666
  // StageAdvancer: the sole stage writer. Held for the rollout job/route to drive;
29566
29667
  // constructed here so the write path (liveConfig + token) is wired in one place.
29567
29668
  const stageAdvancer = new stageMod.StageAdvancer({
@@ -29570,6 +29671,9 @@ export async function startServer(options) {
29570
29671
  readStage: readSessionPoolStage,
29571
29672
  writeStageConfig: (s) => liveConfig.set(guardMod.STAGE_CONFIG_PATH, s, { stageWriteToken: guardMod.STAGE_WRITE_TOKEN }),
29572
29673
  rebalanceProof: () => stageMod.rebalanceProofState(_rebalanceProofStore, Date.now()),
29674
+ ...(pairStartedPool ? {
29675
+ shadowProof: () => pairGreenMod.pairShadowProofState(sessionPoolE2EResultStore, Date.now(), pairPeerExists()),
29676
+ } : {}),
29573
29677
  audit: (event, detail) => console.log(pc.dim(` [stage-advancer] ${event} ${JSON.stringify(detail)}`)),
29574
29678
  });
29575
29679
  // ── Promotion activation ────────────────────────────────────────────────
@@ -29592,6 +29696,7 @@ export async function startServer(options) {
29592
29696
  if (promotionCfg.model === 'auto-climb') {
29593
29697
  const promotionTimer = setInterval(() => {
29594
29698
  try {
29699
+ pairGreenProducer?.tick();
29595
29700
  _sessionPoolPromotionActivation?.autoTick();
29596
29701
  }
29597
29702
  catch (err) {
@@ -29612,6 +29717,19 @@ export async function startServer(options) {
29612
29717
  // shares the E2E result store + boot commit SHA + the slow reconcile tick.
29613
29718
  const fromMod = await import('../core/sessionPoolFailoverRunnerConfig.js');
29614
29719
  const failoverRunnerCfg = fromMod.resolveSessionPoolFailoverRunnerConfig(config.multiMachine?.sessionPool?.failoverRunner, (explicit) => resolveDevAgentGate(explicit, config));
29720
+ // One stage-0 producer only: where the failover runner runs, it decides.
29721
+ if (pairStartedPool && !failoverRunnerCfg.enabled) {
29722
+ const readinessMod = await import('../core/poolReadiness.js');
29723
+ pairGreenProducer = new pairGreenMod.PairGreenProducer({
29724
+ store: sessionPoolE2EResultStore,
29725
+ commitSha: currentSessionPoolBuildIdentity,
29726
+ ready: () => {
29727
+ const deps = readinessMod.getActivePoolReadiness();
29728
+ return !!deps && readinessMod.computePoolReadiness(deps).ready === true;
29729
+ },
29730
+ now: () => Date.now(),
29731
+ });
29732
+ }
29615
29733
  if (failoverRunnerCfg.enabled) {
29616
29734
  // The dry-run SIDE store: a separate signed file the promotion path
29617
29735
  // (StageAdvancer/driver) NEVER reads, so a would-record green captured
@@ -31770,14 +31888,62 @@ export async function startServer(options) {
31770
31888
  },
31771
31889
  });
31772
31890
  };
31773
- const runStuckRecovery = () => {
31891
+ let stuckRecoveryRunning = false;
31892
+ const runStuckRecovery = async () => {
31893
+ if (stuckRecoveryRunning)
31894
+ return;
31895
+ stuckRecoveryRunning = true;
31774
31896
  try {
31897
+ if (!coordinator.holdsLease())
31898
+ return;
31899
+ const remoteEvidence = new Map();
31900
+ if (sessionOwnershipRegistry || _sessionPoolStage() !== 'dark') {
31901
+ const placementReader = new CoherenceJournalReader({ stateDir: config.stateDir });
31902
+ const entries = ledgerForRecovery.reclaimStuck(seamlessness.maxProcessingMs);
31903
+ const topics = [...new Set(entries.map(e => e.topic).filter((t) => !!t))];
31904
+ await Promise.all(topics.map(async (topic) => {
31905
+ let history = [];
31906
+ let failed = true;
31907
+ let failedIds = [];
31908
+ let queriedIds = [];
31909
+ try {
31910
+ const r = await fetch(`http://127.0.0.1:${config.port}/telegram/topics/${topic}/messages?limit=100&scope=pool`, {
31911
+ headers: { Authorization: `Bearer ${config.authToken}` }, signal: AbortSignal.timeout(8_000),
31912
+ });
31913
+ if (r.ok) {
31914
+ const j = await r.json();
31915
+ history = Array.isArray(j.messages) ? j.messages : [];
31916
+ failedIds = (j.pool?.failed ?? []).map(f => f.machineId ?? '');
31917
+ queriedIds = (j.pool?.queried ?? []).filter((id) => typeof id === 'string');
31918
+ failed = !Array.isArray(j.messages) || !Array.isArray(j.pool?.queried);
31919
+ }
31920
+ }
31921
+ catch { /* missing peer evidence remains unknown */ }
31922
+ let priorOwners = [];
31923
+ try {
31924
+ const placements = placementReader.query({ topic: Number(topic), kind: 'topic-placement', limit: 100 });
31925
+ if (placements.truncated || placements.entries.length === 0)
31926
+ failed = true;
31927
+ priorOwners = placements.entries.map(e => e.data.owner)
31928
+ .filter((owner) => typeof owner === 'string' && owner !== _meshSelfId);
31929
+ }
31930
+ catch {
31931
+ failed = true;
31932
+ }
31933
+ for (const entry of entries.filter(e => e.topic === topic)) {
31934
+ const id = Number(entry.dedupeKey.split(':').at(-1));
31935
+ remoteEvidence.set(entry.dedupeKey, Number.isSafeInteger(id) ? crossMachineReplyEvidence(history, id, priorOwners, queriedIds, failedIds, !failed) : 'unknown');
31936
+ }
31937
+ }));
31938
+ }
31775
31939
  const res = recoverStuckMessages({
31776
31940
  ledger: ledgerForRecovery,
31777
31941
  holdsLease: () => coordinator.holdsLease(),
31778
31942
  epoch: coordinator.getLeaseEpoch(),
31779
31943
  maxProcessingMs: seamlessness.maxProcessingMs,
31780
31944
  reinject: reinjectStuck,
31945
+ remoteReplyEvidence: sessionOwnershipRegistry || _sessionPoolStage() !== 'dark'
31946
+ ? entry => remoteEvidence.get(entry.dedupeKey) ?? 'unknown' : undefined,
31781
31947
  // Part D, third site (ownership-follows-live-work): per-topic owner check
31782
31948
  // ON TOP of the existing machine-level holdsLease() gate. SKIP a topic
31783
31949
  // owned by a REACHABLE peer (leave its stuck messages in the ledger for
@@ -31824,9 +31990,12 @@ export async function startServer(options) {
31824
31990
  catch (err) {
31825
31991
  console.error(`[stuck-recovery] ${err instanceof Error ? err.message : err}`);
31826
31992
  }
31993
+ finally {
31994
+ stuckRecoveryRunning = false;
31995
+ }
31827
31996
  };
31828
- runStuckRecovery(); // boot: re-run anything a prior crash left mid-turn
31829
- const stuckTimer = setInterval(runStuckRecovery, Math.max(30_000, seamlessness.maxProcessingMs));
31997
+ void runStuckRecovery(); // boot: re-run anything a prior crash left mid-turn
31998
+ const stuckTimer = setInterval(() => { void runStuckRecovery(); }, Math.max(30_000, seamlessness.maxProcessingMs));
31830
31999
  if (stuckTimer.unref)
31831
32000
  stuckTimer.unref();
31832
32001
  console.log(pc.dim(' Stuck-message recovery active (no-loss half, spec §8 G3a)'));