@llblab/pi-kit 0.29.2 → 0.30.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 (208) hide show
  1. package/BACKLOG.md +2 -1
  2. package/CHANGELOG.md +5 -0
  3. package/README.md +2 -2
  4. package/node_modules/@llblab/pi-telegram/AGENTS.md +9 -7
  5. package/node_modules/@llblab/pi-telegram/BACKLOG.md +1 -18
  6. package/node_modules/@llblab/pi-telegram/CHANGELOG.md +11 -0
  7. package/node_modules/@llblab/pi-telegram/README.md +7 -11
  8. package/node_modules/@llblab/pi-telegram/api/commands.ts +3 -0
  9. package/node_modules/@llblab/pi-telegram/dist/api/commands.d.ts +1 -1
  10. package/node_modules/@llblab/pi-telegram/dist/lib/activity-verbosity.d.ts +1 -5
  11. package/node_modules/@llblab/pi-telegram/dist/lib/activity-verbosity.js +15 -42
  12. package/node_modules/@llblab/pi-telegram/dist/lib/activity.d.ts +9 -0
  13. package/node_modules/@llblab/pi-telegram/dist/lib/activity.js +39 -10
  14. package/node_modules/@llblab/pi-telegram/dist/lib/agent-messages.js +7 -3
  15. package/node_modules/@llblab/pi-telegram/dist/lib/bindings.d.ts +43 -5
  16. package/node_modules/@llblab/pi-telegram/dist/lib/bindings.js +163 -20
  17. package/node_modules/@llblab/pi-telegram/dist/lib/bus-api.d.ts +12 -0
  18. package/node_modules/@llblab/pi-telegram/dist/lib/bus-api.js +182 -79
  19. package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.d.ts +111 -31
  20. package/node_modules/@llblab/pi-telegram/dist/lib/bus-follower.js +1324 -359
  21. package/node_modules/@llblab/pi-telegram/dist/lib/bus-leader.d.ts +41 -91
  22. package/node_modules/@llblab/pi-telegram/dist/lib/bus-leader.js +969 -637
  23. package/node_modules/@llblab/pi-telegram/dist/lib/bus-transport.js +16 -9
  24. package/node_modules/@llblab/pi-telegram/dist/lib/bus.d.ts +215 -18
  25. package/node_modules/@llblab/pi-telegram/dist/lib/bus.js +1048 -327
  26. package/node_modules/@llblab/pi-telegram/dist/lib/channel-posts.d.ts +5 -0
  27. package/node_modules/@llblab/pi-telegram/dist/lib/channel-posts.js +434 -261
  28. package/node_modules/@llblab/pi-telegram/dist/lib/command-templates.d.ts +26 -5
  29. package/node_modules/@llblab/pi-telegram/dist/lib/command-templates.js +92 -176
  30. package/node_modules/@llblab/pi-telegram/dist/lib/commands.d.ts +168 -82
  31. package/node_modules/@llblab/pi-telegram/dist/lib/commands.js +1409 -291
  32. package/node_modules/@llblab/pi-telegram/dist/lib/config.d.ts +1 -8
  33. package/node_modules/@llblab/pi-telegram/dist/lib/config.js +34 -76
  34. package/node_modules/@llblab/pi-telegram/dist/lib/delivery.d.ts +2 -2
  35. package/node_modules/@llblab/pi-telegram/dist/lib/delivery.js +16 -16
  36. package/node_modules/@llblab/pi-telegram/dist/lib/extension.js +639 -171
  37. package/node_modules/@llblab/pi-telegram/dist/lib/generative-app-worker.mjs +19 -7
  38. package/node_modules/@llblab/pi-telegram/dist/lib/generative-apps.d.ts +0 -5
  39. package/node_modules/@llblab/pi-telegram/dist/lib/generative-apps.js +61 -49
  40. package/node_modules/@llblab/pi-telegram/dist/lib/inbound.d.ts +5 -0
  41. package/node_modules/@llblab/pi-telegram/dist/lib/inbound.js +35 -59
  42. package/node_modules/@llblab/pi-telegram/dist/lib/journal.d.ts +33 -3
  43. package/node_modules/@llblab/pi-telegram/dist/lib/journal.js +2063 -782
  44. package/node_modules/@llblab/pi-telegram/dist/lib/lifecycle.d.ts +2 -6
  45. package/node_modules/@llblab/pi-telegram/dist/lib/lifecycle.js +75 -58
  46. package/node_modules/@llblab/pi-telegram/dist/lib/locks.d.ts +29 -3
  47. package/node_modules/@llblab/pi-telegram/dist/lib/locks.js +289 -108
  48. package/node_modules/@llblab/pi-telegram/dist/lib/logging.js +50 -14
  49. package/node_modules/@llblab/pi-telegram/dist/lib/media.d.ts +22 -1
  50. package/node_modules/@llblab/pi-telegram/dist/lib/media.js +46 -28
  51. package/node_modules/@llblab/pi-telegram/dist/lib/menu-model.d.ts +19 -5
  52. package/node_modules/@llblab/pi-telegram/dist/lib/menu-model.js +53 -11
  53. package/node_modules/@llblab/pi-telegram/dist/lib/menu-queue.d.ts +5 -4
  54. package/node_modules/@llblab/pi-telegram/dist/lib/menu-queue.js +45 -63
  55. package/node_modules/@llblab/pi-telegram/dist/lib/menu-settings.d.ts +5 -4
  56. package/node_modules/@llblab/pi-telegram/dist/lib/menu-settings.js +114 -78
  57. package/node_modules/@llblab/pi-telegram/dist/lib/menu-status.d.ts +1 -1
  58. package/node_modules/@llblab/pi-telegram/dist/lib/menu-status.js +8 -25
  59. package/node_modules/@llblab/pi-telegram/dist/lib/menu-thinking.d.ts +6 -1
  60. package/node_modules/@llblab/pi-telegram/dist/lib/menu-thinking.js +14 -21
  61. package/node_modules/@llblab/pi-telegram/dist/lib/menu.d.ts +8 -61
  62. package/node_modules/@llblab/pi-telegram/dist/lib/menu.js +92 -164
  63. package/node_modules/@llblab/pi-telegram/dist/lib/model.d.ts +1 -1
  64. package/node_modules/@llblab/pi-telegram/dist/lib/model.js +2 -16
  65. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-attachments.d.ts +6 -27
  66. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-attachments.js +45 -38
  67. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-buttons.js +22 -16
  68. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-markup.d.ts +0 -2
  69. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-markup.js +51 -64
  70. package/node_modules/@llblab/pi-telegram/dist/lib/outbound-voice.js +6 -3
  71. package/node_modules/@llblab/pi-telegram/dist/lib/outbound.d.ts +6 -6
  72. package/node_modules/@llblab/pi-telegram/dist/lib/outbound.js +61 -51
  73. package/node_modules/@llblab/pi-telegram/dist/lib/ownership.js +6 -2
  74. package/node_modules/@llblab/pi-telegram/dist/lib/paths.d.ts +5 -0
  75. package/node_modules/@llblab/pi-telegram/dist/lib/paths.js +37 -21
  76. package/node_modules/@llblab/pi-telegram/dist/lib/pi.d.ts +2 -0
  77. package/node_modules/@llblab/pi-telegram/dist/lib/pi.js +8 -1
  78. package/node_modules/@llblab/pi-telegram/dist/lib/polling.d.ts +21 -37
  79. package/node_modules/@llblab/pi-telegram/dist/lib/polling.js +59 -50
  80. package/node_modules/@llblab/pi-telegram/dist/lib/preview.js +39 -19
  81. package/node_modules/@llblab/pi-telegram/dist/lib/process-identity.d.ts +24 -0
  82. package/node_modules/@llblab/pi-telegram/dist/lib/process-identity.js +133 -0
  83. package/node_modules/@llblab/pi-telegram/dist/lib/prompts.js +2 -7
  84. package/node_modules/@llblab/pi-telegram/dist/lib/queue.d.ts +14 -16
  85. package/node_modules/@llblab/pi-telegram/dist/lib/queue.js +249 -126
  86. package/node_modules/@llblab/pi-telegram/dist/lib/recovery.d.ts +2 -83
  87. package/node_modules/@llblab/pi-telegram/dist/lib/recovery.js +20 -274
  88. package/node_modules/@llblab/pi-telegram/dist/lib/replies.d.ts +16 -24
  89. package/node_modules/@llblab/pi-telegram/dist/lib/replies.js +99 -116
  90. package/node_modules/@llblab/pi-telegram/dist/lib/routing.d.ts +120 -7
  91. package/node_modules/@llblab/pi-telegram/dist/lib/routing.js +4667 -1152
  92. package/node_modules/@llblab/pi-telegram/dist/lib/runtime.d.ts +1 -11
  93. package/node_modules/@llblab/pi-telegram/dist/lib/sections.d.ts +4 -29
  94. package/node_modules/@llblab/pi-telegram/dist/lib/sections.js +7 -20
  95. package/node_modules/@llblab/pi-telegram/dist/lib/skills.js +4 -3
  96. package/node_modules/@llblab/pi-telegram/dist/lib/status.js +86 -57
  97. package/node_modules/@llblab/pi-telegram/dist/lib/sync.d.ts +1 -3
  98. package/node_modules/@llblab/pi-telegram/dist/lib/sync.js +67 -43
  99. package/node_modules/@llblab/pi-telegram/dist/lib/target.d.ts +4 -0
  100. package/node_modules/@llblab/pi-telegram/dist/lib/target.js +18 -0
  101. package/node_modules/@llblab/pi-telegram/dist/lib/telegram-api.d.ts +18 -13
  102. package/node_modules/@llblab/pi-telegram/dist/lib/telegram-api.js +148 -53
  103. package/node_modules/@llblab/pi-telegram/dist/lib/text-groups.js +4 -39
  104. package/node_modules/@llblab/pi-telegram/dist/lib/thread-cleanup-manager.d.ts +6 -27
  105. package/node_modules/@llblab/pi-telegram/dist/lib/thread-cleanup-manager.js +369 -186
  106. package/node_modules/@llblab/pi-telegram/dist/lib/thread-display.d.ts +1 -2
  107. package/node_modules/@llblab/pi-telegram/dist/lib/thread-display.js +58 -38
  108. package/node_modules/@llblab/pi-telegram/dist/lib/thread-naming.d.ts +28 -8
  109. package/node_modules/@llblab/pi-telegram/dist/lib/thread-naming.js +175 -66
  110. package/node_modules/@llblab/pi-telegram/dist/lib/thread-reconciler.d.ts +42 -16
  111. package/node_modules/@llblab/pi-telegram/dist/lib/thread-reconciler.js +90 -50
  112. package/node_modules/@llblab/pi-telegram/dist/lib/threads.d.ts +82 -20
  113. package/node_modules/@llblab/pi-telegram/dist/lib/threads.js +2247 -855
  114. package/node_modules/@llblab/pi-telegram/dist/lib/time-injection.js +1 -2
  115. package/node_modules/@llblab/pi-telegram/dist/lib/turns.js +1 -19
  116. package/node_modules/@llblab/pi-telegram/dist/lib/updates.d.ts +169 -80
  117. package/node_modules/@llblab/pi-telegram/dist/lib/updates.js +2418 -641
  118. package/node_modules/@llblab/pi-telegram/dist/lib/voice.d.ts +7 -22
  119. package/node_modules/@llblab/pi-telegram/dist/lib/voice.js +7 -22
  120. package/node_modules/@llblab/pi-telegram/dist/lib/wire.js +1 -1
  121. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-admission.d.ts +3 -1
  122. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-admission.js +425 -211
  123. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-identity.js +8 -13
  124. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-retirement.d.ts +1 -1
  125. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-retirement.js +328 -140
  126. package/node_modules/@llblab/pi-telegram/dist/lib/workspace-slots.js +8 -4
  127. package/node_modules/@llblab/pi-telegram/dist/package.json +1 -1
  128. package/node_modules/@llblab/pi-telegram/docs/architecture.md +31 -29
  129. package/node_modules/@llblab/pi-telegram/docs/inbound.md +1 -1
  130. package/node_modules/@llblab/pi-telegram/docs/multi-instance-bus.md +15 -3
  131. package/node_modules/@llblab/pi-telegram/docs/outbound.md +1 -1
  132. package/node_modules/@llblab/pi-telegram/docs/public-api.md +29 -1
  133. package/node_modules/@llblab/pi-telegram/docs/ui-style.md +25 -8
  134. package/node_modules/@llblab/pi-telegram/lib/activity-verbosity.ts +59 -82
  135. package/node_modules/@llblab/pi-telegram/lib/activity.ts +146 -102
  136. package/node_modules/@llblab/pi-telegram/lib/agent-messages.ts +12 -5
  137. package/node_modules/@llblab/pi-telegram/lib/bindings.ts +389 -82
  138. package/node_modules/@llblab/pi-telegram/lib/bus-api.ts +263 -102
  139. package/node_modules/@llblab/pi-telegram/lib/bus-follower.ts +2440 -601
  140. package/node_modules/@llblab/pi-telegram/lib/bus-leader.ts +2209 -1290
  141. package/node_modules/@llblab/pi-telegram/lib/bus-transport.ts +46 -12
  142. package/node_modules/@llblab/pi-telegram/lib/bus.ts +1673 -444
  143. package/node_modules/@llblab/pi-telegram/lib/channel-posts.ts +848 -327
  144. package/node_modules/@llblab/pi-telegram/lib/command-templates.ts +152 -265
  145. package/node_modules/@llblab/pi-telegram/lib/commands.ts +2398 -555
  146. package/node_modules/@llblab/pi-telegram/lib/config.ts +177 -170
  147. package/node_modules/@llblab/pi-telegram/lib/delivery.ts +41 -29
  148. package/node_modules/@llblab/pi-telegram/lib/extension.ts +1206 -433
  149. package/node_modules/@llblab/pi-telegram/lib/generative-app-worker.mjs +19 -7
  150. package/node_modules/@llblab/pi-telegram/lib/generative-apps.ts +324 -211
  151. package/node_modules/@llblab/pi-telegram/lib/inbound.ts +95 -97
  152. package/node_modules/@llblab/pi-telegram/lib/journal.ts +5311 -1763
  153. package/node_modules/@llblab/pi-telegram/lib/keyboard.ts +1 -3
  154. package/node_modules/@llblab/pi-telegram/lib/lifecycle.ts +128 -78
  155. package/node_modules/@llblab/pi-telegram/lib/locks.ts +677 -203
  156. package/node_modules/@llblab/pi-telegram/lib/logging.ts +147 -58
  157. package/node_modules/@llblab/pi-telegram/lib/media.ts +82 -43
  158. package/node_modules/@llblab/pi-telegram/lib/menu-model.ts +85 -18
  159. package/node_modules/@llblab/pi-telegram/lib/menu-queue.ts +88 -129
  160. package/node_modules/@llblab/pi-telegram/lib/menu-settings.ts +260 -155
  161. package/node_modules/@llblab/pi-telegram/lib/menu-status.ts +20 -60
  162. package/node_modules/@llblab/pi-telegram/lib/menu-thinking.ts +41 -56
  163. package/node_modules/@llblab/pi-telegram/lib/menu.ts +112 -285
  164. package/node_modules/@llblab/pi-telegram/lib/model.ts +8 -22
  165. package/node_modules/@llblab/pi-telegram/lib/outbound-attachments.ts +156 -125
  166. package/node_modules/@llblab/pi-telegram/lib/outbound-buttons.ts +76 -49
  167. package/node_modules/@llblab/pi-telegram/lib/outbound-markup.ts +96 -94
  168. package/node_modules/@llblab/pi-telegram/lib/outbound-voice.ts +33 -27
  169. package/node_modules/@llblab/pi-telegram/lib/outbound.ts +125 -125
  170. package/node_modules/@llblab/pi-telegram/lib/ownership.ts +19 -10
  171. package/node_modules/@llblab/pi-telegram/lib/paths.ts +148 -49
  172. package/node_modules/@llblab/pi-telegram/lib/pi.ts +35 -15
  173. package/node_modules/@llblab/pi-telegram/lib/polling.ts +144 -147
  174. package/node_modules/@llblab/pi-telegram/lib/preview.ts +93 -40
  175. package/node_modules/@llblab/pi-telegram/lib/process-identity.ts +164 -0
  176. package/node_modules/@llblab/pi-telegram/lib/prompts.ts +4 -11
  177. package/node_modules/@llblab/pi-telegram/lib/queue.ts +511 -300
  178. package/node_modules/@llblab/pi-telegram/lib/recovery.ts +50 -454
  179. package/node_modules/@llblab/pi-telegram/lib/rendering.ts +1 -3
  180. package/node_modules/@llblab/pi-telegram/lib/replies.ts +248 -209
  181. package/node_modules/@llblab/pi-telegram/lib/routing.ts +8509 -2186
  182. package/node_modules/@llblab/pi-telegram/lib/runtime.ts +1 -12
  183. package/node_modules/@llblab/pi-telegram/lib/sections.ts +24 -89
  184. package/node_modules/@llblab/pi-telegram/lib/setup.ts +1 -3
  185. package/node_modules/@llblab/pi-telegram/lib/skills.ts +9 -4
  186. package/node_modules/@llblab/pi-telegram/lib/status.ts +215 -110
  187. package/node_modules/@llblab/pi-telegram/lib/sync.ts +448 -361
  188. package/node_modules/@llblab/pi-telegram/lib/target.ts +22 -3
  189. package/node_modules/@llblab/pi-telegram/lib/telegram-api.ts +302 -127
  190. package/node_modules/@llblab/pi-telegram/lib/text-groups.ts +5 -39
  191. package/node_modules/@llblab/pi-telegram/lib/thread-cleanup-manager.ts +833 -331
  192. package/node_modules/@llblab/pi-telegram/lib/thread-display.ts +213 -97
  193. package/node_modules/@llblab/pi-telegram/lib/thread-naming.ts +232 -87
  194. package/node_modules/@llblab/pi-telegram/lib/thread-reconciler.ts +155 -72
  195. package/node_modules/@llblab/pi-telegram/lib/threads.ts +5318 -1845
  196. package/node_modules/@llblab/pi-telegram/lib/time-injection.ts +1 -4
  197. package/node_modules/@llblab/pi-telegram/lib/turns.ts +15 -28
  198. package/node_modules/@llblab/pi-telegram/lib/updates.ts +5184 -1612
  199. package/node_modules/@llblab/pi-telegram/lib/voice.ts +7 -35
  200. package/node_modules/@llblab/pi-telegram/lib/wire.ts +1 -1
  201. package/node_modules/@llblab/pi-telegram/lib/workspace-admission.ts +996 -416
  202. package/node_modules/@llblab/pi-telegram/lib/workspace-identity.ts +14 -17
  203. package/node_modules/@llblab/pi-telegram/lib/workspace-retirement.ts +878 -376
  204. package/node_modules/@llblab/pi-telegram/lib/workspace-slots.ts +22 -13
  205. package/node_modules/@llblab/pi-telegram/package.json +1 -1
  206. package/package.json +2 -2
  207. package/node_modules/@llblab/pi-telegram/scripts/check-downgrade.mjs +0 -472
  208. package/node_modules/@llblab/pi-telegram/scripts/measure-workspace.mjs +0 -108
@@ -40,8 +40,13 @@ function isValidSnapshot(
40
40
  const slots = new Set<string>();
41
41
  const keys = new Set<string>();
42
42
  for (const binding of bindings) {
43
- if (!isSlot(binding.slot) || !binding.bindingKey ||
44
- slots.has(binding.slot) || keys.has(binding.bindingKey)) return false;
43
+ if (
44
+ !isSlot(binding.slot) ||
45
+ !binding.bindingKey ||
46
+ slots.has(binding.slot) ||
47
+ keys.has(binding.bindingKey)
48
+ )
49
+ return false;
45
50
  slots.add(binding.slot);
46
51
  keys.add(binding.bindingKey);
47
52
  }
@@ -54,17 +59,21 @@ function eligibleByInactivity(
54
59
  nowMs: number,
55
60
  ): TelegramWorkspaceSlotOccupancy[] {
56
61
  const reserved = new Set(reservedSlots);
57
- return bindings.filter((binding) =>
58
- binding.protection === "eligible" &&
59
- !reserved.has(binding.slot) &&
60
- typeof binding.inactiveSinceMs === "number" &&
61
- Number.isFinite(binding.inactiveSinceMs) &&
62
- binding.inactiveSinceMs >= 0 &&
63
- binding.inactiveSinceMs <= nowMs,
64
- ).sort((left, right) =>
65
- left.inactiveSinceMs! - right.inactiveSinceMs! ||
66
- left.slot.charCodeAt(0) - right.slot.charCodeAt(0),
67
- );
62
+ return bindings
63
+ .filter(
64
+ (binding) =>
65
+ binding.protection === "eligible" &&
66
+ !reserved.has(binding.slot) &&
67
+ typeof binding.inactiveSinceMs === "number" &&
68
+ Number.isFinite(binding.inactiveSinceMs) &&
69
+ binding.inactiveSinceMs >= 0 &&
70
+ binding.inactiveSinceMs <= nowMs,
71
+ )
72
+ .sort(
73
+ (left, right) =>
74
+ left.inactiveSinceMs! - right.inactiveSinceMs! ||
75
+ left.slot.charCodeAt(0) - right.slot.charCodeAt(0),
76
+ );
68
77
  }
69
78
 
70
79
  /** Caller must recheck exact ownership and protected work before retirement. */
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/pi-telegram",
3
- "version": "0.52.2",
3
+ "version": "0.53.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/pi-kit",
3
- "version": "0.29.2",
3
+ "version": "0.30.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -47,7 +47,7 @@
47
47
  "@llblab/pi-codex-usage": "0.12.1",
48
48
  "@llblab/pi-grow-loop": "0.9.0",
49
49
  "@llblab/pi-state-flow": "0.26.4",
50
- "@llblab/pi-telegram": "0.52.2",
50
+ "@llblab/pi-telegram": "0.53.0",
51
51
  "@llblab/skills": "1.15.0"
52
52
  },
53
53
  "bundledDependencies": [
@@ -1,472 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Conservative pre-downgrade journal authority check.
5
- * Usage: node scripts/check-downgrade.mjs [agent-dir]
6
- *
7
- * This is an intentionally simpler schema validator than `lib/journal.ts`.
8
- * It only needs to prove whether unresolved journal authority remains, so it
9
- * errs toward BLOCKED and does not cross-check the rules the runtime owns:
10
- *
11
- * - Operator dispositions are not reconciled against surviving entries
12
- * (`lib/journal.ts` rejects a "discard" disposition whose entry is still
13
- * present).
14
- * - `queueOwner` and `failure` are presence-checked, not deep-validated
15
- * (acquisition shape, retry/terminal derivation, and disposition id
16
- * generation stay in `lib/journal.ts`).
17
- *
18
- * The shared entry rules (keys, state enum, update_id agreement, and
19
- * queue/failure metadata presence) MUST stay in sync with
20
- * `lib/journal.ts` `validateJournalEntry`; `tests/journal-downgrade.test.ts`
21
- * reconciles the two surfaces on those rules.
22
- */
23
-
24
- import {
25
- lstatSync,
26
- readFileSync,
27
- opendirSync,
28
- } from "node:fs";
29
- import { homedir } from "node:os";
30
- import { basename, join, resolve } from "node:path";
31
- import { decodeTelegramSessionDirectoryName, resolveTelegramTempDir } from "../dist/lib/paths.js";
32
-
33
- const MAX_DIRECTORY_ENTRIES = 10_000;
34
- const MAX_FILES = 4_096;
35
- const MAX_BYTES = 64 * 1024 * 1024;
36
- let remainingEntries = MAX_DIRECTORY_ENTRIES;
37
- let remainingFiles = MAX_FILES;
38
- let remainingBytes = MAX_BYTES;
39
- const SESSION_SNAPSHOT_PATTERN = /^(?:journal\.[a-f0-9]{16}|inbox)(?:\.[a-zA-Z0-9._-]+)?\.json$/u;
40
- const SNAPSHOT_PATTERN = /^(?:inbox|follower-inbox-[a-f0-9]{16})(?:\.[a-zA-Z0-9._-]+)?\.json$/u;
41
- const SEGMENT_NAME_PATTERN = /^\d{16}\.json$/u;
42
- const AUTHORITY_STATES = new Set(["pending", "retry-wait", "queued", "failed"]);
43
- const SNAPSHOT_KEYS = new Set([
44
- "version",
45
- "revision",
46
- "profile",
47
- "botIdentity",
48
- "entries",
49
- "operatorDispositions",
50
- ]);
51
- const SEGMENT_KEYS = new Set([
52
- "version",
53
- "revision",
54
- "previousRevision",
55
- "profile",
56
- "botIdentity",
57
- "upsertedEntries",
58
- "removedUpdateIds",
59
- "operatorDispositions",
60
- ]);
61
- const ENTRY_KEYS = new Set([
62
- "updateId",
63
- "update",
64
- "admittedAtMs",
65
- "state",
66
- "queueKind",
67
- "queueReceiptId",
68
- "queueOwner",
69
- "queueHandoff",
70
- "failure",
71
- "nextRetryAtMs",
72
- "terminalAtMs",
73
- "terminalReason",
74
- "terminalFailureId",
75
- ]);
76
- const DISPOSITION_KEYS = new Set([
77
- "failureId",
78
- "updateId",
79
- "action",
80
- "committedAtMs",
81
- "attemptCount",
82
- "failureClass",
83
- "terminalAtMs",
84
- "terminalReason",
85
- ]);
86
-
87
- function resolveAgentDir() {
88
- if (process.argv[2]) return resolve(process.argv[2]);
89
- if (process.env.PI_CODING_AGENT_DIR) {
90
- return resolve(process.env.PI_CODING_AGENT_DIR);
91
- }
92
- const executableName = basename(process.execPath).toLowerCase();
93
- const invokedName = basename(process.argv[1] ?? "").toLowerCase();
94
- return join(
95
- homedir(),
96
- executableName.startsWith("omp") || invokedName.startsWith("omp")
97
- ? ".omp"
98
- : ".pi",
99
- "agent",
100
- );
101
- }
102
-
103
- function block(message) {
104
- console.error(`BLOCKED: ${message}`);
105
- process.exitCode = 1;
106
- }
107
-
108
- function hasOnlyKeys(value, allowed) {
109
- return Object.keys(value).every((key) => allowed.has(key));
110
- }
111
-
112
- function isRecord(value) {
113
- return typeof value === "object" && value !== null && !Array.isArray(value);
114
- }
115
-
116
- function isPositiveInteger(value) {
117
- return Number.isSafeInteger(value) && value > 0;
118
- }
119
-
120
- function isNonNegativeInteger(value) {
121
- return Number.isSafeInteger(value) && value >= 0;
122
- }
123
-
124
- function listDirectory(path) {
125
- const directory = opendirSync(path);
126
- const names = [];
127
- try {
128
- for (;;) {
129
- const entry = directory.readSync();
130
- if (!entry) return names.sort();
131
- if (--remainingEntries < 0) throw new Error("journal directory listing limit exhausted");
132
- names.push(entry.name);
133
- }
134
- } finally { directory.closeSync(); }
135
- }
136
-
137
- function readJson(path, stat) {
138
- try {
139
- if (--remainingFiles < 0 || stat.size > remainingBytes)
140
- throw new Error("journal file/byte inspection limit exhausted");
141
- remainingBytes -= stat.size;
142
- return JSON.parse(readFileSync(path, "utf8"));
143
- } catch (error) {
144
- block(
145
- `cannot verify ${path}: ${error instanceof Error ? error.message : String(error)}`,
146
- );
147
- return undefined;
148
- }
149
- }
150
-
151
- function validateBotIdentity(value) {
152
- return (
153
- isRecord(value) &&
154
- hasOnlyKeys(value, new Set(["botId", "tokenSha256"])) &&
155
- (value.botId === undefined || isPositiveInteger(value.botId)) &&
156
- typeof value.tokenSha256 === "string" &&
157
- /^[a-f0-9]{64}$/u.test(value.tokenSha256)
158
- );
159
- }
160
-
161
- function identitiesMatch(left, right) {
162
- if (
163
- left.botId !== undefined &&
164
- right.botId !== undefined &&
165
- left.botId !== right.botId
166
- ) {
167
- return false;
168
- }
169
- return (
170
- (left.botId !== undefined && left.botId === right.botId) ||
171
- left.tokenSha256 === right.tokenSha256
172
- );
173
- }
174
-
175
- function mergeBotIdentity(stored, current) {
176
- return {
177
- ...(current.botId !== undefined
178
- ? { botId: current.botId }
179
- : stored.botId !== undefined
180
- ? { botId: stored.botId }
181
- : {}),
182
- tokenSha256: current.tokenSha256,
183
- };
184
- }
185
-
186
- function validateEntries(value) {
187
- if (!Array.isArray(value)) return undefined;
188
- const entries = new Map();
189
- let previousId = -1;
190
- for (const entry of value) {
191
- if (
192
- !isRecord(entry) ||
193
- !hasOnlyKeys(entry, ENTRY_KEYS) ||
194
- !isNonNegativeInteger(entry.updateId) ||
195
- entry.updateId <= previousId ||
196
- !AUTHORITY_STATES.has(entry.state) ||
197
- !isRecord(entry.update) ||
198
- entry.update.update_id !== entry.updateId ||
199
- !isNonNegativeInteger(entry.admittedAtMs)
200
- ) {
201
- return undefined;
202
- }
203
- const hasQueueMetadata =
204
- entry.queueKind !== undefined ||
205
- entry.queueReceiptId !== undefined ||
206
- entry.queueOwner !== undefined ||
207
- entry.queueHandoff !== undefined;
208
- const hasFailureMetadata =
209
- entry.failure !== undefined ||
210
- entry.nextRetryAtMs !== undefined ||
211
- entry.terminalAtMs !== undefined ||
212
- entry.terminalReason !== undefined ||
213
- entry.terminalFailureId !== undefined;
214
- if (
215
- (entry.state === "pending" && (hasQueueMetadata || hasFailureMetadata)) ||
216
- (entry.state === "queued" &&
217
- ((entry.queueKind !== "prompt" && entry.queueKind !== "control") ||
218
- typeof entry.queueReceiptId !== "string" ||
219
- entry.queueReceiptId.length === 0 ||
220
- entry.queueOwner === undefined ||
221
- hasFailureMetadata)) ||
222
- ((entry.state === "retry-wait" || entry.state === "failed") &&
223
- (hasQueueMetadata || !isRecord(entry.failure)))
224
- ) {
225
- return undefined;
226
- }
227
- previousId = entry.updateId;
228
- entries.set(entry.updateId, entry);
229
- }
230
- return entries;
231
- }
232
-
233
- function validateOperatorDispositions(value) {
234
- if (value === undefined) return true;
235
- if (!Array.isArray(value)) return false;
236
- const failureIds = new Set();
237
- for (const disposition of value) {
238
- if (
239
- !isRecord(disposition) ||
240
- !hasOnlyKeys(disposition, DISPOSITION_KEYS) ||
241
- typeof disposition.failureId !== "string" ||
242
- disposition.failureId.length === 0 ||
243
- failureIds.has(disposition.failureId) ||
244
- !isNonNegativeInteger(disposition.updateId) ||
245
- (disposition.action !== "retry" && disposition.action !== "discard") ||
246
- !isNonNegativeInteger(disposition.committedAtMs) ||
247
- !isPositiveInteger(disposition.attemptCount) ||
248
- typeof disposition.failureClass !== "string" ||
249
- disposition.failureClass.length === 0 ||
250
- !isNonNegativeInteger(disposition.terminalAtMs) ||
251
- disposition.committedAtMs < disposition.terminalAtMs ||
252
- typeof disposition.terminalReason !== "string" ||
253
- disposition.terminalReason.length === 0
254
- ) {
255
- return false;
256
- }
257
- failureIds.add(disposition.failureId);
258
- }
259
- return true;
260
- }
261
-
262
- function validateSnapshot(value) {
263
- if (
264
- !isRecord(value) ||
265
- !hasOnlyKeys(value, SNAPSHOT_KEYS) ||
266
- value.version !== 1 ||
267
- (value.revision !== undefined && !isPositiveInteger(value.revision)) ||
268
- typeof value.profile !== "string" ||
269
- value.profile.length === 0 ||
270
- !validateBotIdentity(value.botIdentity) ||
271
- !validateOperatorDispositions(value.operatorDispositions)
272
- ) {
273
- return undefined;
274
- }
275
- const entries = validateEntries(value.entries);
276
- if (!entries) return undefined;
277
- return {
278
- revision: value.revision ?? 0,
279
- profile: value.profile,
280
- botIdentity: value.botIdentity,
281
- entries,
282
- };
283
- }
284
-
285
- function validateSegment(value) {
286
- if (
287
- !isRecord(value) ||
288
- !hasOnlyKeys(value, SEGMENT_KEYS) ||
289
- value.version !== 1 ||
290
- !isPositiveInteger(value.revision) ||
291
- !isNonNegativeInteger(value.previousRevision) ||
292
- value.previousRevision !== value.revision - 1 ||
293
- typeof value.profile !== "string" ||
294
- value.profile.length === 0 ||
295
- !validateBotIdentity(value.botIdentity) ||
296
- !Array.isArray(value.upsertedEntries) ||
297
- !Array.isArray(value.removedUpdateIds) ||
298
- !validateOperatorDispositions(value.operatorDispositions)
299
- ) {
300
- return undefined;
301
- }
302
- const upsertedEntries = validateEntries(
303
- [...value.upsertedEntries].sort((left, right) =>
304
- Number(left?.updateId) - Number(right?.updateId),
305
- ),
306
- );
307
- if (!upsertedEntries || upsertedEntries.size !== value.upsertedEntries.length) {
308
- return undefined;
309
- }
310
- const removedUpdateIds = new Set();
311
- for (const updateId of value.removedUpdateIds) {
312
- if (
313
- !isNonNegativeInteger(updateId) ||
314
- removedUpdateIds.has(updateId) ||
315
- upsertedEntries.has(updateId)
316
- ) {
317
- return undefined;
318
- }
319
- removedUpdateIds.add(updateId);
320
- }
321
- return {
322
- revision: value.revision,
323
- previousRevision: value.previousRevision,
324
- profile: value.profile,
325
- botIdentity: value.botIdentity,
326
- upsertedEntries,
327
- removedUpdateIds,
328
- };
329
- }
330
-
331
- const agentDir = resolveAgentDir();
332
- const runtimeDir = resolveTelegramTempDir(agentDir);
333
- let runtimeNames;
334
- let observedRuntime = false;
335
- try {
336
- if (!lstatSync(runtimeDir).isDirectory()) throw new Error("non-directory Telegram runtime or link");
337
- observedRuntime = true;
338
- runtimeNames = listDirectory(runtimeDir);
339
- } catch (error) {
340
- if (error?.code === "ENOENT" && !observedRuntime) {
341
- console.log("SAFE: no Telegram runtime directory exists.");
342
- process.exit(0);
343
- }
344
- block(`cannot verify Telegram runtime directory ${runtimeDir}: ${error instanceof Error ? error.message : String(error)}`);
345
- process.exit(1);
346
- }
347
-
348
- function collectSnapshots(directory, names, pattern, strict = false) {
349
- const snapshots = names.filter(name => pattern.test(name));
350
- const snapshotSet = new Set(snapshots);
351
- for (const name of names) {
352
- // Legacy recovery folders hold no journal authority; current releases delete them.
353
- if (name.toLowerCase() === "recovery") continue;
354
- const snapshotName = name.endsWith(".segments") ? name.slice(0, -".segments".length) : undefined;
355
- if (snapshotName && pattern.test(snapshotName)) {
356
- if (!snapshotSet.has(snapshotName)) block(`cannot verify orphan journal segments without ${join(directory, snapshotName)}`);
357
- continue;
358
- }
359
- if (!pattern.test(name) && (strict || /^(?:inbox|follower-inbox|journal\.)/iu.test(name)))
360
- block(`cannot verify unrecognized journal-like artifact ${join(directory, name)}`);
361
- }
362
- return snapshots.map(name => join(directory, name));
363
- }
364
-
365
- const snapshotPaths = collectSnapshots(runtimeDir, runtimeNames, SNAPSHOT_PATTERN);
366
- for (const name of runtimeNames.filter(name => name.toLowerCase() === "sessions")) {
367
- const root = join(runtimeDir, name);
368
- try {
369
- if (name !== "sessions" || !lstatSync(root).isDirectory()) throw new Error("noncanonical session root or link/type");
370
- for (const sessionName of listDirectory(root)) {
371
- const directory = join(root, sessionName);
372
- if (decodeTelegramSessionDirectoryName(sessionName) === undefined || !lstatSync(directory).isDirectory()) {
373
- block(`cannot verify session folder ${directory}`);
374
- continue;
375
- }
376
- snapshotPaths.push(...collectSnapshots(directory, listDirectory(directory), SESSION_SNAPSHOT_PATTERN, true));
377
- }
378
- } catch (error) { block(`cannot verify session journals ${root}: ${String(error)}`); }
379
- }
380
-
381
- for (const path of snapshotPaths) {
382
- let pathStat;
383
- try {
384
- pathStat = lstatSync(path);
385
- } catch (error) {
386
- block(`cannot verify journal snapshot ${path}: ${String(error)}`);
387
- continue;
388
- }
389
- if (!pathStat.isFile()) {
390
- block(`cannot verify non-file journal snapshot ${path}`);
391
- continue;
392
- }
393
- const snapshot = validateSnapshot(readJson(path, pathStat));
394
- if (!snapshot) {
395
- block(`cannot verify malformed journal snapshot ${path}`);
396
- continue;
397
- }
398
-
399
- let revision = snapshot.revision;
400
- let botIdentity = snapshot.botIdentity;
401
- const entries = snapshot.entries;
402
- const segmentDir = `${path}.segments`;
403
- let segmentDirectoryStat;
404
- try { segmentDirectoryStat = lstatSync(segmentDir); }
405
- catch (error) {
406
- if (error?.code !== "ENOENT") block(`cannot verify journal segments ${segmentDir}: ${String(error)}`);
407
- }
408
- if (segmentDirectoryStat) {
409
- let segmentNames;
410
- try {
411
- if (!segmentDirectoryStat.isDirectory()) {
412
- block(`cannot verify non-directory journal segments ${segmentDir}`);
413
- continue;
414
- }
415
- segmentNames = listDirectory(segmentDir);
416
- } catch (error) {
417
- block(`cannot verify journal segments ${segmentDir}: ${String(error)}`);
418
- continue;
419
- }
420
- const unrecognized = segmentNames.filter(
421
- (segmentName) => !SEGMENT_NAME_PATTERN.test(segmentName),
422
- );
423
- for (const segmentName of unrecognized) {
424
- block(`cannot verify unrecognized journal segment ${join(segmentDir, segmentName)}`);
425
- }
426
- for (const segmentName of segmentNames.filter((candidate) =>
427
- SEGMENT_NAME_PATTERN.test(candidate),
428
- ).sort()) {
429
- const nameRevision = Number(segmentName.slice(0, 16));
430
- const segmentPath = join(segmentDir, segmentName);
431
- let segmentStat;
432
- try {
433
- segmentStat = lstatSync(segmentPath);
434
- } catch (error) {
435
- block(`cannot verify journal segment ${segmentPath}: ${String(error)}`);
436
- break;
437
- }
438
- if (!segmentStat.isFile()) { block(`cannot verify non-file journal segment ${segmentPath}`); break; }
439
- if (nameRevision <= revision) continue;
440
- const segment = validateSegment(readJson(segmentPath, segmentStat));
441
- if (
442
- !segment ||
443
- segment.revision !== nameRevision ||
444
- segment.previousRevision !== revision
445
- ) {
446
- block(`cannot verify malformed or gapped journal segment ${segmentPath}`);
447
- break;
448
- }
449
- if (
450
- segment.profile !== snapshot.profile ||
451
- !identitiesMatch(segment.botIdentity, botIdentity)
452
- ) {
453
- block(`cannot verify foreign journal segment identity ${segmentPath}`);
454
- break;
455
- }
456
- for (const updateId of segment.removedUpdateIds) entries.delete(updateId);
457
- for (const [updateId, entry] of segment.upsertedEntries) {
458
- entries.set(updateId, entry);
459
- }
460
- revision = segment.revision;
461
- botIdentity = mergeBotIdentity(botIdentity, segment.botIdentity);
462
- }
463
- }
464
- if (entries.size > 0) {
465
- block(
466
- `${path} retains ${entries.size} unresolved update(s); drain with a compatible runtime before downgrade.`,
467
- );
468
- }
469
- }
470
-
471
- if (process.exitCode) process.exit(1);
472
- console.log(`SAFE: ${snapshotPaths.length} Telegram journal(s) contain no unresolved updates.`);
@@ -1,108 +0,0 @@
1
- /**
2
- * Measures isolated Thread-store filesystem work at representative sizes.
3
- * Usage: node --experimental-strip-types scripts/measure-workspace.mjs
4
- *
5
- * This assertion-backed fixture uses and removes its own temporary directory.
6
- * It never reads configured profiles, calls Telegram, or measures latency.
7
- */
8
-
9
- import assert from "node:assert/strict";
10
- import fs from "node:fs/promises";
11
- import { syncBuiltinESMExports } from "node:module";
12
- import { tmpdir } from "node:os";
13
- import { join } from "node:path";
14
- import { createTelegramTopicTargetStore } from "../lib/threads.ts";
15
-
16
- const repetitions = 10;
17
- const original = { readFile: fs.readFile, writeFile: fs.writeFile, mkdir: fs.mkdir, rename: fs.rename,
18
- parse: JSON.parse, stringify: JSON.stringify };
19
- let counts;
20
- const rows = [];
21
- const root = await fs.mkdtemp(join(tmpdir(), "pi-telegram-measure-workspace-"));
22
-
23
- async function measure(size, operation, execute, samples = repetitions) {
24
- counts = { reads: 0, readBytes: 0, writes: 0, writeBytes: 0, mkdirs: 0, renames: 0,
25
- parses: 0, stringifies: 0 };
26
- try {
27
- for (let index = 0; index < samples; index++) await execute(index);
28
- const result = { size, operation, repetitions: samples, ...counts };
29
- rows.push(result);
30
- return result;
31
- } finally {
32
- counts = undefined;
33
- }
34
- }
35
-
36
- try {
37
- fs.readFile = async (...args) => {
38
- if (counts) counts.reads++;
39
- const result = await original.readFile(...args);
40
- if (counts) counts.readBytes += Buffer.byteLength(result);
41
- return result;
42
- };
43
- fs.writeFile = async (...args) => {
44
- if (counts) { counts.writes++; counts.writeBytes += Buffer.byteLength(args[1]); }
45
- return original.writeFile(...args);
46
- };
47
- fs.mkdir = async (...args) => {
48
- if (counts) counts.mkdirs++;
49
- return original.mkdir(...args);
50
- };
51
- fs.rename = async (...args) => {
52
- if (counts) counts.renames++;
53
- return original.rename(...args);
54
- };
55
- JSON.parse = (...args) => { if (counts) counts.parses++; return original.parse(...args); };
56
- JSON.stringify = (...args) => { if (counts) counts.stringifies++; return original.stringify(...args); };
57
- syncBuiltinESMExports();
58
-
59
- for (const size of [1, 13, 26]) {
60
- const path = join(root, `${size}.json`);
61
- const store = createTelegramTopicTargetStore({ path, getNowMs: () => 1000 });
62
- for (let index = 0; index < size; index++) {
63
- const instanceId = `fixture-${index}`;
64
- const identity = store.claimWorkspaceIdentity(`/fixture/${index}`, instanceId);
65
- assert.ok(identity);
66
- const target = { chatId: 7, threadId: index + 1 };
67
- assert.ok(store.upsertWorkspaceBinding({ ...identity, target, updatedAtMs: 1000 }, instanceId));
68
- store.upsert({ profileKey: `manual:${instanceId}`, instanceId, slot: identity.slot,
69
- target, status: "active", createdAtMs: 1000, updatedAtMs: 1000 });
70
- }
71
- await store.persist();
72
- assert.equal(new Set(store.listWorkspaceBindings().map((entry) => entry.slot)).size, size);
73
- await measure(size, "cold-load", async () => {
74
- const reader = createTelegramTopicTargetStore({ path });
75
- await reader.load();
76
- assert.equal(reader.list().length, size);
77
- assert.equal(reader.listWorkspaceBindings().length, size);
78
- });
79
- const lookup = await measure(size, "lookup-last", () => {
80
- assert.equal(store.getByProfileKey(`manual:fixture-${size - 1}`)?.target.threadId, size);
81
- });
82
- assert.equal(lookup.reads + lookup.writes, 0);
83
- const beforeReloadSave = JSON.parse(await fs.readFile(path, "utf8"));
84
- const reloaded = await measure(size, "first-persist-after-reload", () => store.persist(), 1);
85
- assert.equal(reloaded.writes + reloaded.renames + reloaded.mkdirs, 0);
86
- assert.deepEqual(JSON.parse(await fs.readFile(path, "utf8")), beforeReloadSave,
87
- "The first reloaded save must preserve the seeded semantic state");
88
- const unchanged = await measure(size, "unchanged-persist", () => store.persist());
89
- assert.equal(unchanged.writes + unchanged.renames + unchanged.mkdirs, 0);
90
- assert.ok(unchanged.reads >= repetitions, "No-op saves still consult disk authority");
91
- const changed = await measure(size, "diagnostic-persist", async (index) => {
92
- store.setStatusSnapshot({ diagnostics: { measurementStep: index } });
93
- await store.persist();
94
- });
95
- assert.equal(changed.writes, repetitions);
96
- assert.equal(changed.renames, repetitions);
97
- }
98
- } finally {
99
- Object.assign(fs, { readFile: original.readFile, writeFile: original.writeFile,
100
- mkdir: original.mkdir, rename: original.rename });
101
- JSON.parse = original.parse;
102
- JSON.stringify = original.stringify;
103
- syncBuiltinESMExports();
104
- await fs.rm(root, { recursive: true, force: true });
105
- }
106
- console.log(JSON.stringify({ scope: "Isolated Thread store, not IPC/admission/Telegram or throughput",
107
- counters: "Aggregate API calls and bytes for each row's repetitions; object-spread clones are not instrumented",
108
- rows }, null, 2));