@llblab/pi-kit 0.27.6 → 0.28.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.
Files changed (182) hide show
  1. package/BACKLOG.md +2 -5
  2. package/CHANGELOG.md +5 -0
  3. package/README.md +2 -2
  4. package/node_modules/@llblab/pi-telegram/AGENTS.md +10 -8
  5. package/node_modules/@llblab/pi-telegram/BACKLOG.md +1 -9
  6. package/node_modules/@llblab/pi-telegram/CHANGELOG.md +13 -0
  7. package/node_modules/@llblab/pi-telegram/LICENSE +21 -0
  8. package/node_modules/@llblab/pi-telegram/README.md +15 -5
  9. package/node_modules/@llblab/pi-telegram/dist/lib/activity-verbosity.d.ts +1 -5
  10. package/node_modules/@llblab/pi-telegram/dist/lib/activity-verbosity.js +5 -7
  11. package/node_modules/@llblab/pi-telegram/dist/lib/bindings.d.ts +5 -1
  12. package/node_modules/@llblab/pi-telegram/dist/lib/bindings.js +6 -1
  13. package/node_modules/@llblab/pi-telegram/dist/lib/bus-api.js +4 -2
  14. package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.d.ts +54 -56
  15. package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.js +206 -139
  16. package/node_modules/@llblab/pi-telegram/dist/lib/bus-leader.d.ts +21 -2
  17. package/node_modules/@llblab/pi-telegram/dist/lib/bus-leader.js +181 -25
  18. package/node_modules/@llblab/pi-telegram/dist/lib/bus-transport.d.ts +4 -1
  19. package/node_modules/@llblab/pi-telegram/dist/lib/bus-transport.js +28 -4
  20. package/node_modules/@llblab/pi-telegram/dist/lib/bus.d.ts +52 -56
  21. package/node_modules/@llblab/pi-telegram/dist/lib/bus.js +78 -246
  22. package/node_modules/@llblab/pi-telegram/dist/lib/channel-posts.d.ts +1 -3
  23. package/node_modules/@llblab/pi-telegram/dist/lib/channel-posts.js +23 -26
  24. package/node_modules/@llblab/pi-telegram/dist/lib/command-templates.d.ts +0 -5
  25. package/node_modules/@llblab/pi-telegram/dist/lib/command-templates.js +5 -5
  26. package/node_modules/@llblab/pi-telegram/dist/lib/commands.d.ts +0 -23
  27. package/node_modules/@llblab/pi-telegram/dist/lib/commands.js +15 -15
  28. package/node_modules/@llblab/pi-telegram/dist/lib/config.d.ts +0 -17
  29. package/node_modules/@llblab/pi-telegram/dist/lib/config.js +12 -14
  30. package/node_modules/@llblab/pi-telegram/dist/lib/delivery.d.ts +0 -4
  31. package/node_modules/@llblab/pi-telegram/dist/lib/delivery.js +2 -5
  32. package/node_modules/@llblab/pi-telegram/dist/lib/extension.js +255 -61
  33. package/node_modules/@llblab/pi-telegram/dist/lib/generative-apps.d.ts +0 -19
  34. package/node_modules/@llblab/pi-telegram/dist/lib/inbound.d.ts +0 -2
  35. package/node_modules/@llblab/pi-telegram/dist/lib/inbound.js +2 -2
  36. package/node_modules/@llblab/pi-telegram/dist/lib/journal.d.ts +177 -16
  37. package/node_modules/@llblab/pi-telegram/dist/lib/journal.js +1115 -249
  38. package/node_modules/@llblab/pi-telegram/dist/lib/keyboard.d.ts +0 -2
  39. package/node_modules/@llblab/pi-telegram/dist/lib/keyboard.js +2 -2
  40. package/node_modules/@llblab/pi-telegram/dist/lib/lifecycle.js +1 -1
  41. package/node_modules/@llblab/pi-telegram/dist/lib/locks.d.ts +100 -12
  42. package/node_modules/@llblab/pi-telegram/dist/lib/locks.js +380 -14
  43. package/node_modules/@llblab/pi-telegram/dist/lib/logging.d.ts +30 -12
  44. package/node_modules/@llblab/pi-telegram/dist/lib/logging.js +129 -72
  45. package/node_modules/@llblab/pi-telegram/dist/lib/media.d.ts +28 -5
  46. package/node_modules/@llblab/pi-telegram/dist/lib/media.js +26 -8
  47. package/node_modules/@llblab/pi-telegram/dist/lib/menu-model.d.ts +0 -11
  48. package/node_modules/@llblab/pi-telegram/dist/lib/menu-model.js +8 -8
  49. package/node_modules/@llblab/pi-telegram/dist/lib/menu-settings.d.ts +0 -18
  50. package/node_modules/@llblab/pi-telegram/dist/lib/menu-settings.js +18 -18
  51. package/node_modules/@llblab/pi-telegram/dist/lib/menu-status.d.ts +4 -4
  52. package/node_modules/@llblab/pi-telegram/dist/lib/menu-status.js +12 -7
  53. package/node_modules/@llblab/pi-telegram/dist/lib/menu-thinking.d.ts +1 -1
  54. package/node_modules/@llblab/pi-telegram/dist/lib/menu-thinking.js +1 -1
  55. package/node_modules/@llblab/pi-telegram/dist/lib/menu.d.ts +1 -0
  56. package/node_modules/@llblab/pi-telegram/dist/lib/menu.js +4 -3
  57. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-attachments.d.ts +0 -1
  58. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-attachments.js +1 -1
  59. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-buttons.js +1 -8
  60. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-markup.d.ts +1 -6
  61. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-markup.js +4 -21
  62. package/node_modules/@llblab/pi-telegram/dist/lib/outbound.d.ts +0 -8
  63. package/node_modules/@llblab/pi-telegram/dist/lib/outbound.js +5 -5
  64. package/node_modules/@llblab/pi-telegram/dist/lib/paths.d.ts +50 -7
  65. package/node_modules/@llblab/pi-telegram/dist/lib/paths.js +155 -17
  66. package/node_modules/@llblab/pi-telegram/dist/lib/pi.d.ts +1 -0
  67. package/node_modules/@llblab/pi-telegram/dist/lib/pi.js +2 -1
  68. package/node_modules/@llblab/pi-telegram/dist/lib/polling.d.ts +0 -5
  69. package/node_modules/@llblab/pi-telegram/dist/lib/polling.js +5 -5
  70. package/node_modules/@llblab/pi-telegram/dist/lib/preview.d.ts +0 -3
  71. package/node_modules/@llblab/pi-telegram/dist/lib/preview.js +3 -3
  72. package/node_modules/@llblab/pi-telegram/dist/lib/prompt-templates.d.ts +0 -1
  73. package/node_modules/@llblab/pi-telegram/dist/lib/prompt-templates.js +1 -1
  74. package/node_modules/@llblab/pi-telegram/dist/lib/queue.d.ts +3 -15
  75. package/node_modules/@llblab/pi-telegram/dist/lib/queue.js +69 -8
  76. package/node_modules/@llblab/pi-telegram/dist/lib/recovery.d.ts +29 -9
  77. package/node_modules/@llblab/pi-telegram/dist/lib/recovery.js +104 -33
  78. package/node_modules/@llblab/pi-telegram/dist/lib/replies.d.ts +0 -4
  79. package/node_modules/@llblab/pi-telegram/dist/lib/replies.js +1 -1
  80. package/node_modules/@llblab/pi-telegram/dist/lib/routing.d.ts +73 -8
  81. package/node_modules/@llblab/pi-telegram/dist/lib/routing.js +2206 -307
  82. package/node_modules/@llblab/pi-telegram/dist/lib/sections.d.ts +0 -2
  83. package/node_modules/@llblab/pi-telegram/dist/lib/sections.js +1 -1
  84. package/node_modules/@llblab/pi-telegram/dist/lib/status.d.ts +44 -9
  85. package/node_modules/@llblab/pi-telegram/dist/lib/status.js +149 -27
  86. package/node_modules/@llblab/pi-telegram/dist/lib/sync.d.ts +0 -5
  87. package/node_modules/@llblab/pi-telegram/dist/lib/sync.js +5 -4
  88. package/node_modules/@llblab/pi-telegram/dist/lib/telegram-api.d.ts +11 -3
  89. package/node_modules/@llblab/pi-telegram/dist/lib/telegram-api.js +34 -23
  90. package/node_modules/@llblab/pi-telegram/dist/lib/thread-cleanup-manager.d.ts +1 -0
  91. package/node_modules/@llblab/pi-telegram/dist/lib/thread-cleanup-manager.js +18 -21
  92. package/node_modules/@llblab/pi-telegram/dist/lib/thread-naming.d.ts +32 -5
  93. package/node_modules/@llblab/pi-telegram/dist/lib/thread-naming.js +191 -7
  94. package/node_modules/@llblab/pi-telegram/dist/lib/thread-reconciler.d.ts +2 -0
  95. package/node_modules/@llblab/pi-telegram/dist/lib/thread-reconciler.js +1 -1
  96. package/node_modules/@llblab/pi-telegram/dist/lib/threads.d.ts +291 -59
  97. package/node_modules/@llblab/pi-telegram/dist/lib/threads.js +1647 -369
  98. package/node_modules/@llblab/pi-telegram/dist/lib/turns.d.ts +0 -1
  99. package/node_modules/@llblab/pi-telegram/dist/lib/turns.js +1 -1
  100. package/node_modules/@llblab/pi-telegram/dist/lib/updates.d.ts +184 -28
  101. package/node_modules/@llblab/pi-telegram/dist/lib/updates.js +1032 -114
  102. package/node_modules/@llblab/pi-telegram/dist/lib/wire.d.ts +12 -0
  103. package/node_modules/@llblab/pi-telegram/dist/lib/wire.js +21 -0
  104. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-admission.d.ts +25 -17
  105. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-admission.js +95 -20
  106. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-identity.d.ts +20 -0
  107. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-identity.js +103 -0
  108. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-retirement.d.ts +43 -8
  109. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-retirement.js +148 -52
  110. package/node_modules/@llblab/pi-telegram/dist/package.json +6 -6
  111. package/node_modules/@llblab/pi-telegram/dist/skills/generated-control-surface/SKILL.md +3 -3
  112. package/node_modules/@llblab/pi-telegram/dist/skills/telegram-bridge/references/diagnosis.md +3 -3
  113. package/node_modules/@llblab/pi-telegram/docs/README.md +1 -1
  114. package/node_modules/@llblab/pi-telegram/docs/activity.md +1 -1
  115. package/node_modules/@llblab/pi-telegram/docs/architecture.md +152 -34
  116. package/node_modules/@llblab/pi-telegram/docs/callback-namespaces.md +1 -1
  117. package/node_modules/@llblab/pi-telegram/docs/delivery.md +1 -1
  118. package/node_modules/@llblab/pi-telegram/docs/generative-apps.md +2 -2
  119. package/node_modules/@llblab/pi-telegram/docs/inbound.md +1 -1
  120. package/node_modules/@llblab/pi-telegram/docs/multi-instance-bus.md +125 -19
  121. package/node_modules/@llblab/pi-telegram/docs/public-api.md +3 -1
  122. package/node_modules/@llblab/pi-telegram/docs/ui-style.md +22 -8
  123. package/node_modules/@llblab/pi-telegram/docs/updates.md +1 -1
  124. package/node_modules/@llblab/pi-telegram/lib/activity-verbosity.ts +5 -9
  125. package/node_modules/@llblab/pi-telegram/lib/bindings.ts +6 -0
  126. package/node_modules/@llblab/pi-telegram/lib/bus-api.ts +5 -2
  127. package/node_modules/@llblab/pi-telegram/lib/bus-follower.ts +243 -224
  128. package/node_modules/@llblab/pi-telegram/lib/bus-leader.ts +178 -31
  129. package/node_modules/@llblab/pi-telegram/lib/bus-transport.ts +30 -8
  130. package/node_modules/@llblab/pi-telegram/lib/bus.ts +110 -334
  131. package/node_modules/@llblab/pi-telegram/lib/channel-posts.ts +29 -29
  132. package/node_modules/@llblab/pi-telegram/lib/command-templates.ts +5 -5
  133. package/node_modules/@llblab/pi-telegram/lib/commands.ts +15 -15
  134. package/node_modules/@llblab/pi-telegram/lib/config.ts +12 -17
  135. package/node_modules/@llblab/pi-telegram/lib/delivery.ts +2 -8
  136. package/node_modules/@llblab/pi-telegram/lib/extension.ts +257 -72
  137. package/node_modules/@llblab/pi-telegram/lib/generative-apps.ts +0 -22
  138. package/node_modules/@llblab/pi-telegram/lib/inbound.ts +2 -2
  139. package/node_modules/@llblab/pi-telegram/lib/journal.ts +1182 -326
  140. package/node_modules/@llblab/pi-telegram/lib/keyboard.ts +2 -2
  141. package/node_modules/@llblab/pi-telegram/lib/lifecycle.ts +1 -1
  142. package/node_modules/@llblab/pi-telegram/lib/locks.ts +402 -23
  143. package/node_modules/@llblab/pi-telegram/lib/logging.ts +151 -101
  144. package/node_modules/@llblab/pi-telegram/lib/media.ts +51 -9
  145. package/node_modules/@llblab/pi-telegram/lib/menu-model.ts +8 -8
  146. package/node_modules/@llblab/pi-telegram/lib/menu-settings.ts +18 -18
  147. package/node_modules/@llblab/pi-telegram/lib/menu-status.ts +11 -0
  148. package/node_modules/@llblab/pi-telegram/lib/menu-thinking.ts +2 -2
  149. package/node_modules/@llblab/pi-telegram/lib/menu.ts +5 -1
  150. package/node_modules/@llblab/pi-telegram/lib/outbound-attachments.ts +1 -1
  151. package/node_modules/@llblab/pi-telegram/lib/outbound-buttons.ts +1 -10
  152. package/node_modules/@llblab/pi-telegram/lib/outbound-markup.ts +5 -24
  153. package/node_modules/@llblab/pi-telegram/lib/outbound.ts +5 -5
  154. package/node_modules/@llblab/pi-telegram/lib/paths.ts +177 -17
  155. package/node_modules/@llblab/pi-telegram/lib/pi.ts +5 -2
  156. package/node_modules/@llblab/pi-telegram/lib/polling.ts +5 -5
  157. package/node_modules/@llblab/pi-telegram/lib/preview.ts +3 -3
  158. package/node_modules/@llblab/pi-telegram/lib/prompt-templates.ts +1 -1
  159. package/node_modules/@llblab/pi-telegram/lib/queue.ts +67 -9
  160. package/node_modules/@llblab/pi-telegram/lib/recovery.ts +104 -44
  161. package/node_modules/@llblab/pi-telegram/lib/replies.ts +1 -1
  162. package/node_modules/@llblab/pi-telegram/lib/routing.ts +1955 -375
  163. package/node_modules/@llblab/pi-telegram/lib/sections.ts +1 -1
  164. package/node_modules/@llblab/pi-telegram/lib/status.ts +156 -36
  165. package/node_modules/@llblab/pi-telegram/lib/sync.ts +4 -4
  166. package/node_modules/@llblab/pi-telegram/lib/telegram-api.ts +48 -33
  167. package/node_modules/@llblab/pi-telegram/lib/thread-cleanup-manager.ts +24 -21
  168. package/node_modules/@llblab/pi-telegram/lib/thread-naming.ts +267 -9
  169. package/node_modules/@llblab/pi-telegram/lib/thread-reconciler.ts +3 -1
  170. package/node_modules/@llblab/pi-telegram/lib/threads.ts +1697 -488
  171. package/node_modules/@llblab/pi-telegram/lib/turns.ts +1 -1
  172. package/node_modules/@llblab/pi-telegram/lib/updates.ts +1072 -151
  173. package/node_modules/@llblab/pi-telegram/lib/wire.ts +28 -0
  174. package/node_modules/@llblab/pi-telegram/lib/workspace-admission.ts +97 -47
  175. package/node_modules/@llblab/pi-telegram/lib/workspace-identity.ts +147 -0
  176. package/node_modules/@llblab/pi-telegram/lib/workspace-retirement.ts +188 -89
  177. package/node_modules/@llblab/pi-telegram/package.json +6 -6
  178. package/node_modules/@llblab/pi-telegram/scripts/audit-exports.mjs +100 -0
  179. package/node_modules/@llblab/pi-telegram/scripts/check-downgrade.mjs +80 -43
  180. package/node_modules/@llblab/pi-telegram/skills/generated-control-surface/SKILL.md +3 -3
  181. package/node_modules/@llblab/pi-telegram/skills/telegram-bridge/references/diagnosis.md +3 -3
  182. package/package.json +2 -2
@@ -17,7 +17,5 @@ export type TelegramInlineKeyboardButton = {
17
17
  export interface TelegramInlineKeyboardMarkup {
18
18
  inline_keyboard: TelegramInlineKeyboardButton[][];
19
19
  }
20
- export declare const TELEGRAM_CALLBACK_DATA_MAX_BYTES = 64;
21
- export declare function getTelegramCallbackDataByteLength(value: string): number;
22
20
  export declare function assertTelegramCallbackData(callbackData: string, context?: string): string;
23
21
  export declare function assertTelegramInlineKeyboardCallbackData(replyMarkup: unknown, context?: string): void;
@@ -3,8 +3,8 @@
3
3
  * Zones: telegram ui, shared structure
4
4
  * Owns the shared Bot API reply-markup shape while feature domains own their button semantics
5
5
  */
6
- export const TELEGRAM_CALLBACK_DATA_MAX_BYTES = 64;
7
- export function getTelegramCallbackDataByteLength(value) {
6
+ const TELEGRAM_CALLBACK_DATA_MAX_BYTES = 64;
7
+ function getTelegramCallbackDataByteLength(value) {
8
8
  return new TextEncoder().encode(value).byteLength;
9
9
  }
10
10
  export function assertTelegramCallbackData(callbackData, context = "Telegram callback_data") {
@@ -343,7 +343,7 @@ export function createTelegramCompactionObserverRuntime(deps) {
343
343
  typingStartedByObserver = false;
344
344
  deps.updateStatus(ctx);
345
345
  deps.recordRuntimeEvent?.("compact", new Error("Compaction observer timed out"));
346
- deps.onCompactionAbandoned?.();
346
+ // Observer expiry releases local presence, not Pi's eventual terminal result.
347
347
  requestDispatch();
348
348
  }, timeoutMs);
349
349
  unrefTelegramLifecycleTimer(fallbackTimer);
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Telegram singleton lock helpers
3
3
  * Zones: telegram ownership, filesystem, transport authority
4
- * Owns extension-local owners.json access and Telegram bridge ownership semantics
4
+ * Owns filesystem authority, atomic runtime-section publication and Telegram bridge ownership semantics
5
5
  */
6
6
  import { renameSync } from "node:fs";
7
7
  export declare const TELEGRAM_LOCK_KEY = "default";
@@ -14,10 +14,21 @@ export declare const TELEGRAM_OWNERSHIP_REFRESH_MS = 2000;
14
14
  * Named profile → the validated profile name
15
15
  */
16
16
  export declare function resolveTelegramLockKey(activeProfile?: string): string;
17
- export interface TelegramActiveProfileGetter {
17
+ interface TelegramActiveProfileGetter {
18
18
  getActiveProfileName: () => string | undefined;
19
19
  }
20
20
  export declare function createTelegramLockKeyResolver(activeProfile: TelegramActiveProfileGetter): () => string;
21
+ /** Structural session view; Locks never imports the lifecycle owner. */
22
+ interface TelegramOwnedStateSessionPort<TContext extends TelegramLockContext> {
23
+ get: () => TContext | undefined;
24
+ getGeneration: () => number;
25
+ isCurrent: (ctx: TContext, generation?: number) => boolean;
26
+ }
27
+ /**
28
+ * Captures exact context, session generation and owned leader epoch before awaits.
29
+ * Session replacement, release or re-election revokes the grant; a successor never renews it.
30
+ */
31
+ export declare function createTelegramOwnedStateAuthorityCapture<TContext extends TelegramLockContext>(lock: Pick<TelegramLockRuntime<TContext>, "owns" | "getOwnedLeaderEpoch">, session: TelegramOwnedStateSessionPort<TContext>): () => (() => boolean) | undefined;
21
32
  export interface TelegramLockEntry {
22
33
  pid: number;
23
34
  cwd?: string;
@@ -27,6 +38,8 @@ export interface TelegramLockEntry {
27
38
  runtimeGeneration?: number;
28
39
  busSocketPath?: string;
29
40
  busSecret?: string;
41
+ /** Polling journal custody; inherited by every successor and kept after release. */
42
+ journalPath?: string;
30
43
  }
31
44
  export interface TelegramLockContext {
32
45
  cwd: string;
@@ -43,12 +56,12 @@ export type TelegramLockState = {
43
56
  kind: "stale";
44
57
  lock: TelegramLockEntry;
45
58
  };
46
- export interface TelegramLockAcquireOptions {
59
+ interface TelegramLockAcquireOptions {
47
60
  force?: boolean;
48
61
  expectedOwner?: TelegramLockEntry;
49
62
  election?: boolean;
50
63
  }
51
- export type TelegramLockAcquireResult = {
64
+ type TelegramLockAcquireResult = {
52
65
  ok: true;
53
66
  lock: TelegramLockEntry;
54
67
  replacedStale: boolean;
@@ -56,6 +69,12 @@ export type TelegramLockAcquireResult = {
56
69
  ok: false;
57
70
  lock: TelegramLockEntry;
58
71
  };
72
+ export type TelegramOwnedStatePublicationResult<T> = {
73
+ committed: false;
74
+ } | {
75
+ committed: true;
76
+ result: T;
77
+ };
59
78
  export interface TelegramLockRuntime<TContext extends TelegramLockContext> {
60
79
  acquire: (ctx: TContext, options?: TelegramLockAcquireOptions) => TelegramLockAcquireResult;
61
80
  release: () => TelegramLockState;
@@ -64,17 +83,26 @@ export interface TelegramLockRuntime<TContext extends TelegramLockContext> {
64
83
  getOwnedLeaderEpoch: () => number | string | undefined;
65
84
  owns: (ctx?: TelegramLockContext) => boolean;
66
85
  commitIfOwned: (commit: () => void) => boolean;
86
+ /** Consolidated-store publication; domain reducers retain their own exact payload/CAS rules. */
87
+ publishStateSectionIfOwned?: <T>(section: "workspace" | "runtime", mutate: (current: unknown, observed: Readonly<TelegramRuntimeStateProfile>) => TelegramRuntimeStateMutation<T>, options: TelegramOwnedStatePublicationOptions) => TelegramOwnedStatePublicationResult<T>;
67
88
  refresh: (ctx?: TelegramLockContext) => boolean;
89
+ /** The polling journal named by owners.json for this key, owned or not. */
90
+ getJournalPath: () => string | undefined;
68
91
  }
69
- export interface TelegramLockOwnershipGuard<TContext extends TelegramLockContext> {
92
+ interface TelegramLockOwnershipGuard<TContext extends TelegramLockContext> {
70
93
  ownsContext: (ctx: TContext) => boolean;
71
94
  }
72
- export interface TelegramLockContextStore<TContext extends TelegramLockContext> {
95
+ interface TelegramLockContextStore<TContext extends TelegramLockContext> {
73
96
  get: () => TContext | undefined;
74
97
  }
75
- export interface TelegramLockRuntimeOptions {
98
+ interface TelegramLockRuntimeOptions {
76
99
  key?: string | (() => string | undefined);
77
100
  locksPath?: string;
101
+ /** Consolidated version-2 state envelope; exclusive with `locksPath`. */
102
+ statePath?: string;
103
+ statePublication?: Pick<TelegramRuntimeStatePublicationOptions, "onPublicationBoundary" | "publishRename">;
104
+ /** Read-only ownership file of releases that used another directory; a live fresh owner there blocks acquisition. */
105
+ legacyLocksPath?: string;
78
106
  pid?: number;
79
107
  isProcessAlive?: (pid: number) => boolean;
80
108
  instanceId?: string;
@@ -84,9 +112,25 @@ export interface TelegramLockRuntimeOptions {
84
112
  mintLeaderEpoch?: () => number | string;
85
113
  runtimeGeneration?: number;
86
114
  staleHeartbeatMs?: number;
115
+ /** First leader without an inherited pointer names the polling journal it creates. */
116
+ createJournalPath?: (ctx: TelegramLockContext) => string | undefined;
87
117
  }
118
+ /**
119
+ * Leader polling journal: the path owners.json names, else the session that would host it on acquisition.
120
+ * Without a session identity, the flat root `inbox` remains the compatibility fallback.
121
+ */
122
+ export declare function createTelegramLeaderJournalPathResolver(deps: {
123
+ getNamedJournalPath: () => string | undefined;
124
+ getSessionId: () => string | undefined;
125
+ getProfileName: () => string | undefined;
126
+ }): {
127
+ createJournalPath: () => string | undefined;
128
+ resolve(profileName?: string): string;
129
+ };
130
+ /** A released key keeps only `{ journalPath }`: no owner, but the successor's polling custody. */
131
+ export declare function readTelegramLockJournalPath(value: unknown): string | undefined;
88
132
  export declare function readLocks(path?: string): Record<string, unknown>;
89
- export interface TelegramRenameRetryOptions {
133
+ interface TelegramRenameRetryOptions {
90
134
  rename?: typeof renameSync;
91
135
  attempts?: number;
92
136
  retryDelayMs?: number;
@@ -100,17 +144,56 @@ export interface TelegramFileTransactionOptions {
100
144
  retryDelayMs?: number;
101
145
  }
102
146
  export declare function withTelegramFileTransaction<T>(transactionPath: string, operation: () => T, options?: TelegramFileTransactionOptions): T;
147
+ export type TelegramRuntimeStateSection = "transport" | "workspace" | "admission" | "runtime";
148
+ type TelegramRuntimeStateProfile = Partial<Record<TelegramRuntimeStateSection, unknown>>;
149
+ interface TelegramRuntimeStateFile {
150
+ version: 2;
151
+ profiles: Record<string, TelegramRuntimeStateProfile>;
152
+ }
153
+ export declare class TelegramRuntimeStateError extends Error {
154
+ readonly code: "invalid" | "authority-changed" | "publication-unknown";
155
+ constructor(code: TelegramRuntimeStateError["code"], message: string, options?: ErrorOptions);
156
+ }
157
+ /** Strict, observational envelope read; section owners validate their own payloads. Legacy state is never adopted here. */
158
+ export declare function readTelegramRuntimeState(path: string): TelegramRuntimeStateFile;
159
+ /**
160
+ * Operator-approved optimistic recovery before leader acquisition: when the shared envelope, any transport section or a
161
+ * caller-validated section is damaged, publish a fresh empty envelope instead of refusing. All profiles lose runtime
162
+ * continuity. Filesystem access errors are not damage and still throw. Returns whether a reset was published.
163
+ */
164
+ export declare function resetDamagedTelegramRuntimeState(path: string, validateProfile?: (profile: string, sections: Readonly<TelegramRuntimeStateProfile>) => void): boolean;
165
+ export interface TelegramRuntimeStateMutation<T> {
166
+ value: unknown;
167
+ result: T;
168
+ }
169
+ interface TelegramRuntimeStatePublicationOptions {
170
+ /** Exact domain authority, recaptured by the caller before invoking this synchronous transaction. */
171
+ isCurrent: () => boolean;
172
+ onPublicationBoundary?: (boundary: "before-write" | "after-write-before-rename" | "after-rename") => void;
173
+ publishRename?: typeof renameSync;
174
+ }
175
+ export interface TelegramOwnedStatePublicationOptions extends TelegramRuntimeStatePublicationOptions {
176
+ /** Optional caller-bound physical/logical identity; a mismatch cannot redirect an owner grant. */
177
+ expectedScope?: {
178
+ path: string;
179
+ profile: string;
180
+ };
181
+ }
182
+ /**
183
+ * One physical read/check/write transaction for one named section. The caller supplies domain policy; copies expose
184
+ * current sibling facts without granting writes to them. No await, nested transaction, repair or legacy import.
185
+ */
186
+ export declare function mutateTelegramRuntimeStateSection<T>(path: string, profile: string, section: TelegramRuntimeStateSection, mutate: (current: unknown, observed: Readonly<TelegramRuntimeStateProfile>) => TelegramRuntimeStateMutation<T>, options: TelegramRuntimeStatePublicationOptions): T;
103
187
  export declare function writeLocks(path: string, locks: Record<string, unknown>): void;
104
188
  export declare function parseTelegramLockEntry(value: unknown): TelegramLockEntry | undefined;
105
189
  export declare function isProcessAlive(pid: number): boolean;
106
- export declare function formatTelegramLockEntry(lock: TelegramLockEntry): string;
107
190
  export declare function createTelegramLockRuntime<TContext extends TelegramLockContext>(options?: TelegramLockRuntimeOptions): TelegramLockRuntime<TContext>;
108
191
  export declare function createTelegramLockOwnershipGuard<TContext extends TelegramLockContext>(lock: TelegramLockRuntime<TContext>): TelegramLockOwnershipGuard<TContext>;
109
192
  export declare function createTelegramDirectDeliveryOwnershipChecker<TContext extends TelegramLockContext>(deps: {
110
193
  lock: TelegramLockRuntime<TContext>;
111
194
  contextStore: TelegramLockContextStore<TContext>;
112
195
  }): () => boolean;
113
- export interface TelegramLockedPollingStartOptions {
196
+ interface TelegramLockedPollingStartOptions {
114
197
  force?: boolean;
115
198
  forceFreshLeaderThread?: boolean;
116
199
  requestedThreadName?: string;
@@ -119,7 +202,7 @@ export interface TelegramLockedPollingStartOptions {
119
202
  };
120
203
  onAcquired?: () => Promise<void> | void;
121
204
  }
122
- export type TelegramLockedPollingStartResult = {
205
+ type TelegramLockedPollingStartResult = {
123
206
  ok: true;
124
207
  message?: string;
125
208
  canTakeover?: false;
@@ -134,14 +217,18 @@ export interface TelegramLockedPollingRuntime<TContext extends TelegramLockConte
134
217
  stop: () => Promise<string>;
135
218
  suspend: () => Promise<void>;
136
219
  isSuspended: () => boolean;
220
+ /** Fence one owned polling generation; suspension, restart, conflict or lock loss revokes it. */
221
+ captureTransportAuthority: (ctx: TContext) => (() => boolean) | undefined;
137
222
  onPersistentConflict: (ctx: TContext, count: number) => Promise<void>;
138
223
  onSessionStart: (_event: unknown, ctx: TContext) => Promise<void>;
139
224
  registerFollowerWithOwner?: (ctx: TContext, owner: TelegramLockEntry) => boolean | undefined | Promise<boolean | undefined>;
140
225
  restoreFollowerWithOwner?: (ctx: TContext, owner: TelegramLockEntry) => boolean | undefined | Promise<boolean | undefined>;
141
226
  stopFollowerRegistration?: () => void;
142
227
  }
143
- export interface TelegramLockedPollingRuntimeDeps<TContext extends TelegramLockContext> {
228
+ interface TelegramLockedPollingRuntimeDeps<TContext extends TelegramLockContext> {
144
229
  lock: TelegramLockRuntime<TContext>;
230
+ /** Optimistically replaces a damaged shared runtime state before a non-election acquisition. */
231
+ resetDamagedState?: () => boolean;
145
232
  hasBotToken: () => boolean;
146
233
  getBotTokenDiagnostic?: () => string | undefined;
147
234
  canStartPolling?: (ctx: TContext) => boolean;
@@ -163,3 +250,4 @@ export interface TelegramLockedPollingRuntimeDeps<TContext extends TelegramLockC
163
250
  ownershipRefreshMs?: number;
164
251
  }
165
252
  export declare function createTelegramLockedPollingRuntime<TContext extends TelegramLockContext>(deps: TelegramLockedPollingRuntimeDeps<TContext>): TelegramLockedPollingRuntime<TContext>;
253
+ export {};