typeclaw 0.50.0 → 0.52.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 (258) hide show
  1. package/dist/types/agent/bash-sandbox-env.d.ts +17 -0
  2. package/dist/types/agent/bash-sandbox-proc.d.ts +8 -0
  3. package/dist/types/agent/bash-sandbox.d.ts +98 -0
  4. package/dist/types/agent/compaction.d.ts +4 -4
  5. package/dist/types/agent/idle-compaction.d.ts +1 -0
  6. package/dist/types/agent/index.d.ts +10 -17
  7. package/dist/types/agent/multimodal/look-at.d.ts +1 -1
  8. package/dist/types/agent/output-allowance.d.ts +4 -0
  9. package/dist/types/agent/plugin-tools.d.ts +8 -75
  10. package/dist/types/agent/prompt-cache-key.d.ts +1 -0
  11. package/dist/types/agent/session-tail.d.ts +12 -0
  12. package/dist/types/agent/subagents.d.ts +5 -0
  13. package/dist/types/agent/system-prompt-composition.d.ts +18 -0
  14. package/dist/types/agent/system-prompt.d.ts +2 -0
  15. package/dist/types/agent/tools/channel-disengage.d.ts +1 -1
  16. package/dist/types/agent/tools/channel-edit.d.ts +1 -1
  17. package/dist/types/agent/tools/channel-fetch-attachment.d.ts +1 -1
  18. package/dist/types/agent/tools/channel-handoff.d.ts +1 -1
  19. package/dist/types/agent/tools/channel-history.d.ts +1 -1
  20. package/dist/types/agent/tools/channel-react.d.ts +1 -1
  21. package/dist/types/agent/tools/channel-read.d.ts +10 -2
  22. package/dist/types/agent/tools/channel-reply.d.ts +1 -1
  23. package/dist/types/agent/tools/channel-send.d.ts +1 -1
  24. package/dist/types/agent/tools/channel-session-search.d.ts +1 -1
  25. package/dist/types/agent/tools/ddg.d.ts +9 -0
  26. package/dist/types/agent/tools/grant-role.d.ts +1 -1
  27. package/dist/types/agent/tools/post-github-review.d.ts +1 -1
  28. package/dist/types/agent/tools/restart.d.ts +1 -1
  29. package/dist/types/agent/tools/skip-response.d.ts +1 -1
  30. package/dist/types/agent/tools/spawn-subagent.d.ts +2 -1
  31. package/dist/types/agent/tools/stream-snapshot.d.ts +2 -2
  32. package/dist/types/agent/tools/subagent-cancel.d.ts +1 -1
  33. package/dist/types/agent/tools/subagent-output.d.ts +1 -1
  34. package/dist/types/agent/tools/todo/index.d.ts +1 -1
  35. package/dist/types/agent/tools/webfetch/adaptive-html.d.ts +3 -3
  36. package/dist/types/agent/tools/webfetch/strategies/readability-process.d.ts +48 -0
  37. package/dist/types/agent/tools/webfetch/tool.d.ts +2 -2
  38. package/dist/types/agent/tools/websearch.d.ts +1 -1
  39. package/dist/types/channels/adapters/github/auth-app.d.ts +25 -0
  40. package/dist/types/channels/adapters/github/auth-pat.d.ts +17 -0
  41. package/dist/types/channels/adapters/github/auth.d.ts +24 -0
  42. package/dist/types/channels/github-review-check.d.ts +1 -1
  43. package/dist/types/channels/github-token-bridge.d.ts +4 -0
  44. package/dist/types/channels/lost-background-context.d.ts +2 -0
  45. package/dist/types/channels/persistence.d.ts +1 -0
  46. package/dist/types/channels/router.d.ts +16 -50
  47. package/dist/types/channels/types.d.ts +1 -0
  48. package/dist/types/coding-agent/tool.d.ts +1 -1
  49. package/dist/types/config/config.d.ts +22 -0
  50. package/dist/types/config/providers.d.ts +50 -1
  51. package/dist/types/container/start.d.ts +1 -0
  52. package/dist/types/eval/cell.d.ts +26 -0
  53. package/dist/types/eval/config.d.ts +19 -0
  54. package/dist/types/eval/dispatch.d.ts +7 -0
  55. package/dist/types/eval/kernel.d.ts +28 -0
  56. package/dist/types/eval/process.d.ts +23 -0
  57. package/dist/types/eval/protocol.d.ts +34 -0
  58. package/dist/types/eval/sandbox.d.ts +12 -0
  59. package/dist/types/eval/tool.d.ts +17 -0
  60. package/dist/types/eval/wire.d.ts +33 -0
  61. package/dist/types/git/resolve-identity.d.ts +2 -0
  62. package/dist/types/init/dockerfile.d.ts +1 -0
  63. package/dist/types/init/gitignore.d.ts +1 -1
  64. package/dist/types/permissions/builtins.d.ts +1 -0
  65. package/dist/types/plugin/types.d.ts +3 -0
  66. package/dist/types/sandbox/canonical-secrets.d.ts +2 -2
  67. package/dist/types/sandbox/coding-agent.d.ts +1 -1
  68. package/dist/types/sandbox/dependency-bins.d.ts +1 -0
  69. package/dist/types/sandbox/dev-source.d.ts +2 -2
  70. package/dist/types/sandbox/env-exposure.d.ts +2 -0
  71. package/dist/types/sandbox/hidden-paths.d.ts +1 -0
  72. package/dist/types/sandbox/index.d.ts +1 -1
  73. package/dist/types/sandbox/policy.d.ts +3 -0
  74. package/dist/types/typevault/cli-path.d.ts +2 -0
  75. package/dist/types/typevault/envelope-store.d.ts +11 -0
  76. package/dist/types/typevault/lifecycle.d.ts +13 -0
  77. package/dist/types/typevault/vault.d.ts +27 -0
  78. package/package.json +8 -7
  79. package/scripts/test.ts +252 -0
  80. package/src/agent/bash-sandbox-env.ts +136 -0
  81. package/src/agent/bash-sandbox-proc.ts +70 -0
  82. package/src/agent/bash-sandbox.ts +239 -0
  83. package/src/agent/compaction.ts +22 -10
  84. package/src/agent/idle-compaction.ts +7 -3
  85. package/src/agent/index.ts +185 -187
  86. package/src/agent/multimodal/look-at.ts +1 -1
  87. package/src/agent/output-allowance.ts +7 -0
  88. package/src/agent/plugin-tools.ts +64 -380
  89. package/src/agent/prompt-cache-key.ts +26 -3
  90. package/src/agent/session-origin.ts +41 -19
  91. package/src/agent/session-tail.ts +28 -0
  92. package/src/agent/subagent-completion-reminder.ts +2 -2
  93. package/src/agent/subagents.ts +21 -2
  94. package/src/agent/system-prompt-composition.ts +159 -0
  95. package/src/agent/system-prompt.ts +43 -5
  96. package/src/agent/todo/scope.ts +1 -1
  97. package/src/agent/tool-error-remediation.ts +18 -3
  98. package/src/agent/tools/channel-disengage.ts +1 -1
  99. package/src/agent/tools/channel-edit.ts +1 -1
  100. package/src/agent/tools/channel-fetch-attachment.ts +1 -1
  101. package/src/agent/tools/channel-handoff.ts +3 -2
  102. package/src/agent/tools/channel-history.ts +2 -1
  103. package/src/agent/tools/channel-react.ts +1 -1
  104. package/src/agent/tools/channel-read.ts +36 -3
  105. package/src/agent/tools/channel-reply.ts +1 -1
  106. package/src/agent/tools/channel-send.ts +4 -3
  107. package/src/agent/tools/channel-session-search.ts +13 -4
  108. package/src/agent/tools/ddg.ts +71 -11
  109. package/src/agent/tools/grant-role.ts +1 -1
  110. package/src/agent/tools/post-github-review.ts +1 -1
  111. package/src/agent/tools/restart.ts +1 -1
  112. package/src/agent/tools/skip-response.ts +1 -1
  113. package/src/agent/tools/spawn-subagent.ts +15 -1
  114. package/src/agent/tools/stream-snapshot.ts +1 -1
  115. package/src/agent/tools/subagent-cancel.ts +1 -1
  116. package/src/agent/tools/subagent-output.ts +1 -1
  117. package/src/agent/tools/todo/index.ts +1 -1
  118. package/src/agent/tools/webfetch/adaptive-html.ts +6 -4
  119. package/src/agent/tools/webfetch/strategies/readability-child.ts +25 -0
  120. package/src/agent/tools/webfetch/strategies/readability-process.ts +185 -0
  121. package/src/agent/tools/webfetch/tool.ts +6 -5
  122. package/src/agent/tools/websearch.ts +1 -1
  123. package/src/agent/tools-without-local-file-operands.ts +4 -0
  124. package/src/browser/dockerfile.ts +1 -1
  125. package/src/browser/fill-secret.ts +364 -0
  126. package/src/browser/registry.ts +6 -6
  127. package/src/bundled-plugins/agent-messenger-cli-auth/command.ts +211 -0
  128. package/src/bundled-plugins/agent-messenger-cli-auth/index.ts +186 -0
  129. package/src/bundled-plugins/backup/index.ts +17 -2
  130. package/src/bundled-plugins/backup/runner.ts +37 -2
  131. package/src/bundled-plugins/browser-agent/browser-agent.ts +51 -14
  132. package/src/bundled-plugins/browser-agent/index.ts +12 -5
  133. package/src/bundled-plugins/explorer/explorer.ts +1 -1
  134. package/src/bundled-plugins/github-cli-auth/gh-review-detect.ts +40 -16
  135. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +12 -7
  136. package/src/bundled-plugins/github-cli-auth/index.ts +10 -1
  137. package/src/bundled-plugins/managed-browser/index.ts +130 -2
  138. package/src/bundled-plugins/managed-browser/skills/browserskill/browserskill/SKILL.md +13 -5
  139. package/src/bundled-plugins/managed-browser/skills/omowright/omowright/SKILL.md +56 -5
  140. package/src/bundled-plugins/memory/README.md +23 -2
  141. package/src/bundled-plugins/memory/append-fragment.ts +133 -0
  142. package/src/bundled-plugins/memory/append-tool.ts +80 -149
  143. package/src/bundled-plugins/memory/dreaming.ts +6 -1
  144. package/src/bundled-plugins/memory/index.ts +33 -6
  145. package/src/bundled-plugins/memory/load-memory.ts +2 -4
  146. package/src/bundled-plugins/memory/memory-logger.ts +200 -6
  147. package/src/bundled-plugins/memory/references/store-reference-tool.ts +56 -32
  148. package/src/bundled-plugins/memory/remember-tool.ts +81 -0
  149. package/src/bundled-plugins/memory/search-tool-hybrid.ts +91 -0
  150. package/src/bundled-plugins/memory/search-tool.ts +58 -12
  151. package/src/bundled-plugins/memory/stream-events.ts +2 -0
  152. package/src/bundled-plugins/memory/transcript-cursor.ts +30 -0
  153. package/src/bundled-plugins/memory/transcript-preload.ts +208 -0
  154. package/src/bundled-plugins/memory/transcript-view.ts +36 -0
  155. package/src/bundled-plugins/memory/vector/hybrid.ts +25 -14
  156. package/src/bundled-plugins/memory/watermark.ts +19 -10
  157. package/src/bundled-plugins/reviewer/reviewer.ts +20 -0
  158. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +3 -6
  159. package/src/bundled-plugins/security/policies/outbound-text.ts +20 -0
  160. package/src/bundled-plugins/security/policies/private-surface-read.ts +166 -201
  161. package/src/bundled-plugins/security/policies/system-prompt-leak.ts +2 -7
  162. package/src/bundled-plugins/typevault/grants.ts +119 -0
  163. package/src/bundled-plugins/typevault/index.ts +152 -0
  164. package/src/bundled-plugins/typevault/request.ts +87 -0
  165. package/src/bundled-plugins/typevault/runtime.ts +45 -0
  166. package/src/bundled-plugins/typevault/skills/typevault/SKILL.md +65 -0
  167. package/src/channels/adapter-halt.ts +17 -0
  168. package/src/channels/adapters/discord-bot-reference.ts +5 -4
  169. package/src/channels/adapters/discord-bot.ts +1 -0
  170. package/src/channels/adapters/discord-classify.ts +8 -3
  171. package/src/channels/adapters/discord.ts +29 -0
  172. package/src/channels/adapters/github/history.ts +23 -2
  173. package/src/channels/adapters/github/inbound.ts +60 -1
  174. package/src/channels/adapters/github/index.ts +27 -5
  175. package/src/channels/adapters/github/reconcile-open-prs.ts +7 -1
  176. package/src/channels/adapters/kakaotalk-author-resolver.ts +7 -5
  177. package/src/channels/adapters/kakaotalk-channel-resolver.ts +45 -15
  178. package/src/channels/adapters/kakaotalk-classify.ts +7 -9
  179. package/src/channels/adapters/kakaotalk-list.ts +62 -0
  180. package/src/channels/adapters/kakaotalk-member-cache.ts +67 -0
  181. package/src/channels/adapters/kakaotalk-membership.ts +27 -10
  182. package/src/channels/adapters/kakaotalk-mention.ts +61 -0
  183. package/src/channels/adapters/kakaotalk.ts +159 -125
  184. package/src/channels/adapters/teams-classify.ts +18 -8
  185. package/src/channels/github-review-check.ts +5 -1
  186. package/src/channels/github-review-verdict-coordinator.ts +68 -2
  187. package/src/channels/github-token-bridge.ts +13 -0
  188. package/src/channels/lost-background-context.ts +46 -0
  189. package/src/channels/manager.ts +52 -3
  190. package/src/channels/persistence.ts +5 -0
  191. package/src/channels/router.ts +208 -128
  192. package/src/channels/schema.ts +1 -1
  193. package/src/channels/subagent-completion-bridge.ts +3 -2
  194. package/src/channels/types.ts +3 -0
  195. package/src/cli/hostd.ts +3 -0
  196. package/src/coding-agent/tool.ts +1 -1
  197. package/src/config/config.ts +35 -1
  198. package/src/config/providers.ts +75 -4
  199. package/src/container/start.ts +52 -3
  200. package/src/doctor/channel-checks.ts +33 -0
  201. package/src/doctor/checks.ts +37 -18
  202. package/src/doctor/index.ts +48 -12
  203. package/src/doctor/operational-incidents.ts +16 -1
  204. package/src/doctor/plugin-bridge.ts +14 -1
  205. package/src/doctor/stage.ts +14 -0
  206. package/src/doctor/types.ts +6 -0
  207. package/src/eval/cell.ts +93 -0
  208. package/src/eval/config.ts +32 -0
  209. package/src/eval/dispatch.ts +67 -0
  210. package/src/eval/entry.ts +261 -0
  211. package/src/eval/kernel.ts +154 -0
  212. package/src/eval/process.ts +113 -0
  213. package/src/eval/protocol.ts +26 -0
  214. package/src/eval/redeclare.ts +174 -0
  215. package/src/eval/sandbox.ts +117 -0
  216. package/src/eval/tool.ts +99 -0
  217. package/src/eval/wire.ts +93 -0
  218. package/src/git/resolve-identity.ts +67 -2
  219. package/src/git/system-commit.ts +7 -0
  220. package/src/init/dockerfile.ts +5 -1
  221. package/src/init/gitignore.ts +3 -3
  222. package/src/init/index.ts +1 -0
  223. package/src/init/models-dev.ts +2 -0
  224. package/src/init/validate-api-key.ts +5 -0
  225. package/src/migrations/secrets-v1-to-v2.ts +2 -0
  226. package/src/operations/classify-tool-outcome.ts +1 -1
  227. package/src/permissions/builtins.ts +8 -0
  228. package/src/plugin/core-tool-names.ts +6 -1
  229. package/src/plugin/types.ts +6 -0
  230. package/src/run/bundled-plugins.ts +12 -0
  231. package/src/run/channel-session-factory.ts +3 -0
  232. package/src/run/index.ts +13 -2
  233. package/src/sandbox/build.ts +1 -0
  234. package/src/sandbox/canonical-secrets.ts +6 -0
  235. package/src/sandbox/dependency-bins.ts +13 -1
  236. package/src/sandbox/dev-source.ts +21 -12
  237. package/src/sandbox/env-exposure.ts +20 -0
  238. package/src/sandbox/hidden-paths.ts +1 -1
  239. package/src/sandbox/index.ts +1 -0
  240. package/src/sandbox/policy.ts +5 -0
  241. package/src/secrets/hydrate.ts +4 -1
  242. package/src/secrets/storage.ts +10 -1
  243. package/src/server/command-runner.ts +4 -0
  244. package/src/server/index.ts +54 -12
  245. package/src/skills/typeclaw-config/SKILL.md +36 -2
  246. package/src/skills/typeclaw-memory/SKILL.md +3 -1
  247. package/src/skills/typeclaw-permissions/SKILL.md +8 -8
  248. package/src/skills/typeclaw-troubleshooting/SKILL.md +18 -1
  249. package/src/tunnels/active.ts +14 -0
  250. package/src/typevault/bash-cli.ts +18 -0
  251. package/src/typevault/cli-path.ts +11 -0
  252. package/src/typevault/envelope-helper.ts +17 -0
  253. package/src/typevault/envelope-store.ts +98 -0
  254. package/src/typevault/lifecycle.ts +132 -0
  255. package/src/typevault/prompt-tunnel.ts +33 -0
  256. package/src/typevault/redaction.ts +37 -0
  257. package/src/typevault/vault.ts +283 -0
  258. package/typeclaw.schema.json +92 -10
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { ChannelRouter } from '@/channels/router'
5
5
  import { ADAPTER_IDS, type AdapterId } from '@/channels/schema'
@@ -8,12 +8,24 @@ import type { ChannelKey, ChannelListEntry } from '@/channels/types'
8
8
  import { renderHistoryMessage, renderHistoryMessages } from './channel-history-render'
9
9
  import { type ChannelToolLogger, consoleChannelLogger, formatChannelToolFailure } from './channel-log'
10
10
 
11
+ // `chat` is absent for mode "list", which enumerates the whole workspace and
12
+ // so never counts as the speaker's own conversation.
13
+ export type ChannelReadTarget = { adapter: AdapterId; workspace: string; chat?: string; thread?: string }
14
+
11
15
  export type CreateChannelReadToolOptions = {
12
16
  router: ChannelRouter
13
17
  logger?: ChannelToolLogger
14
18
  sessionId?: string
19
+ // Decides per call whether the current speaker may read `target`. Absent
20
+ // means no gate (composition tests without a permission service).
21
+ canRead?: (target: ChannelReadTarget) => boolean
15
22
  }
16
23
 
24
+ const READ_DENIED =
25
+ "reading a conversation other than the current one requires the `fs.see.private` permission, which the current speaker's role does not have. " +
26
+ 'Hand the request to its owning session with channel_session_search and channel_handoff instead. ' +
27
+ 'An operator can grant the permission through `roles` in typeclaw.json.'
28
+
17
29
  const DEFAULT_LIMIT = 20
18
30
 
19
31
  // channel_read is the arbitrary-addressing read counterpart to channel_send:
@@ -27,6 +39,7 @@ export function createChannelReadTool({
27
39
  router,
28
40
  logger = consoleChannelLogger,
29
41
  sessionId,
42
+ canRead,
30
43
  }: CreateChannelReadToolOptions) {
31
44
  return defineTool({
32
45
  name: 'channel_read',
@@ -38,9 +51,11 @@ export function createChannelReadTool({
38
51
  '- "message": fetch ONE message from `chat` by `message_id`. Use for "what do you think of <message id>".\n' +
39
52
  '- "list": discover channels/chats in `workspace` (no `chat` needed). Use when you do not yet know a chat id.\n' +
40
53
  'For Discord guild channels, `workspace` is the guild id; for Slack team channels, `workspace` is the team id (e.g. "T0ACME"); for DMs, "@dm". ' +
54
+ 'For KakaoTalk, `workspace` is "@kakao-dm", "@kakao-group", or "@kakao-open" (open chats). ' +
41
55
  'On failure returns { ok: false, error }. Not every adapter supports every mode — an unsupported mode returns a not-supported error rather than throwing. ' +
42
- 'This is read-only. When work belongs to another conversation, use channel_session_search and channel_handoff ' +
43
- 'instead of reading or posting there directly. An access error may include an authorized handoff target.',
56
+ 'This is read-only. Reading a conversation other than the current one (including mode "list") requires the ' +
57
+ 'speaker to hold `fs.see.private`; when it is denied, use channel_session_search and channel_handoff instead. ' +
58
+ 'An access error may include an authorized handoff target.',
44
59
  parameters: Type.Object({
45
60
  mode: Type.Union([Type.Literal('history'), Type.Literal('message'), Type.Literal('list')], {
46
61
  description:
@@ -87,6 +102,23 @@ export function createChannelReadTool({
87
102
  async execute(_toolCallId, params) {
88
103
  const adapter = params.adapter
89
104
  const limit = params.limit ?? DEFAULT_LIMIT
105
+ if (
106
+ canRead !== undefined &&
107
+ !canRead({
108
+ adapter,
109
+ workspace: params.workspace,
110
+ ...(params.mode !== 'list' && params.chat !== undefined ? { chat: params.chat } : {}),
111
+ ...(params.mode !== 'list' && params.thread !== undefined ? { thread: params.thread } : {}),
112
+ })
113
+ ) {
114
+ logger.warn(
115
+ formatChannelToolFailure(
116
+ 'channel_read',
117
+ `${adapter}:${params.workspace}/${params.chat ?? '*'}: denied, speaker lacks fs.see.private`,
118
+ ),
119
+ )
120
+ return fail(READ_DENIED)
121
+ }
90
122
 
91
123
  let result: { content: { type: 'text'; text: string }[]; details: Details }
92
124
  switch (params.mode) {
@@ -159,6 +191,7 @@ async function readHistory(args: {
159
191
  return fail('mode "history" requires `chat`.')
160
192
  }
161
193
  const result = await router.fetchHistory(adapter, {
194
+ workspace: params.workspace,
162
195
  chat: params.chat,
163
196
  thread: params.thread ?? null,
164
197
  limit,
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { BackgroundObligationRef } from '@/channels/background-obligations'
5
5
  import { checkCompletionClaim } from '@/channels/completion-claim'
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { BackgroundObligationRef } from '@/channels/background-obligations'
5
5
  import { checkFalseReceipt } from '@/channels/github-false-receipt'
@@ -66,8 +66,9 @@ export function createChannelSendTool({
66
66
  'or use `channel_reply`. A returned id starts a thread only where the adapter accepts a message ' +
67
67
  'id as `thread` (e.g. a new top-level Slack post); other adapters use different thread ids. ' +
68
68
  'Some adapters do not report ids. ' +
69
- 'For work belonging to another conversation, use channel_session_search and channel_handoff so the owning ' +
70
- 'session answers with current context. Use this tool for an explicit direct post, not to take over that work.',
69
+ 'For work belonging to another conversation, prefer channel_session_search and channel_handoff so its live ' +
70
+ 'owning session answers with current context. Post there directly only when no live session owns it and ' +
71
+ '`channel_read` of that conversation is allowed for this speaker; otherwise use the handoff path.',
71
72
  parameters: Type.Object({
72
73
  adapter: Type.Union(
73
74
  ADAPTER_IDS.map((a) => Type.Literal(a)),
@@ -1,8 +1,16 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { ChannelRouter } from '@/channels/router'
5
5
 
6
+ // Idle sessions are evicted and cross-platform identities rarely resolve a role
7
+ // in the target, so an empty result is common. Without a next step here the
8
+ // model reads it as "unreachable" and stops.
9
+ const NO_TARGETS_TEXT =
10
+ 'No eligible live sessions found. The conversation may still be reachable: read it directly with `channel_read` ' +
11
+ '(`mode: "list"` finds chat ids) and, if that read is allowed, post there with `channel_send`. If `channel_read` ' +
12
+ "is denied, don't post there; tell the speaker no live session owns that conversation."
13
+
6
14
  export function createChannelSessionSearchTool(options: {
7
15
  router: Pick<ChannelRouter, 'searchSessions'>
8
16
  sessionId: string
@@ -14,8 +22,9 @@ export function createChannelSessionSearchTool(options: {
14
22
  'Find the session that owns another conversation across all adapters. Search conversation names, recent ' +
15
23
  'messages, or participants in any language. Returns up to 20 authorized, live handoff targets with short ' +
16
24
  'activity hints, newest first. For work belonging elsewhere, search here and pass a result target to ' +
17
- 'channel_handoff instead of reading or posting there yourself. Identity comes from your authenticated ' +
18
- 'requester, never from the query. No results means no eligible live session was found.',
25
+ 'channel_handoff instead of posting there yourself. Identity comes from your authenticated requester, ' +
26
+ 'never from the query. Only live, recently active sessions are listed, so no results does not mean the ' +
27
+ 'conversation is unreachable.',
19
28
  parameters: Type.Object({
20
29
  query: Type.String({ description: 'Conversation name, topic, message text, or participant.', minLength: 1 }),
21
30
  }),
@@ -25,7 +34,7 @@ export function createChannelSessionSearchTool(options: {
25
34
  content: [
26
35
  {
27
36
  type: 'text' as const,
28
- text: targets.length === 0 ? 'No eligible live sessions found.' : JSON.stringify(targets),
37
+ text: targets.length === 0 ? NO_TARGETS_TEXT : JSON.stringify(targets),
29
38
  },
30
39
  ],
31
40
  details: { targets },
@@ -40,6 +40,41 @@ const searchLimiter = createKeyedSemaphore({ concurrency: DDG_CONCURRENCY })
40
40
 
41
41
  const DDG_KEY = 'ddg'
42
42
 
43
+ // The concurrency cap bounds parallelism, not rate: a subagent fan-out of ~10
44
+ // searches inside 30s still tripped DDG's gate behind a cap of 2 (production
45
+ // incident, 2026-10-27). Spacing every outbound request by a minimum interval
46
+ // turns such a burst into a queue instead of a volley.
47
+ export const DDG_MIN_REQUEST_INTERVAL_MS = 1_500
48
+
49
+ // Once DDG serves a challenge to every profile the source IP is boxed for
50
+ // minutes, and each further request keeps the box alive. Stop sending for a
51
+ // cooldown window: calls inside it fail fast without touching the network.
52
+ export const DDG_CAPTCHA_COOLDOWN_MS = 5 * 60_000
53
+
54
+ type Pacing = {
55
+ intervalMs: number
56
+ cooldownMs: number
57
+ now: () => number
58
+ sleep: (ms: number, signal?: AbortSignal) => Promise<void>
59
+ }
60
+ const DEFAULT_PACING: Pacing = {
61
+ intervalMs: DDG_MIN_REQUEST_INTERVAL_MS,
62
+ cooldownMs: DDG_CAPTCHA_COOLDOWN_MS,
63
+ now: () => Date.now(),
64
+ sleep: abortableSleep,
65
+ }
66
+ let pacing: Pacing = DEFAULT_PACING
67
+ let nextRequestAt = 0
68
+ let cooldownUntil = 0
69
+
70
+ // Test-only seam: swaps the clock/sleep and interval/cooldown, and resets the
71
+ // process-wide pacing state so tests don't leak a cooldown into each other.
72
+ export function _setDdgPacingForTest(override: Partial<Pacing> | null): void {
73
+ pacing = { ...DEFAULT_PACING, ...override }
74
+ nextRequestAt = 0
75
+ cooldownUntil = 0
76
+ }
77
+
43
78
  export type DdgResult = {
44
79
  title: string
45
80
  url: string
@@ -63,9 +98,14 @@ export async function ddgSearch(query: string, limit: number, signal?: AbortSign
63
98
  withSearchRetry(
64
99
  async () => {
65
100
  for (const binary of DDG_PROFILES) {
101
+ if (pacing.now() < cooldownUntil) throw new DdgCaptchaError()
102
+ await waitForRequestSlot(signal)
103
+ // A concurrent search may have started the cooldown while this one waited.
104
+ if (pacing.now() < cooldownUntil) throw new DdgCaptchaError()
66
105
  const html = await fetchDdgHtml(query, signal, binary)
67
106
  if (!isCaptcha(html)) return parseDdgHtml(html).slice(0, limit)
68
107
  }
108
+ cooldownUntil = pacing.now() + pacing.cooldownMs
69
109
  throw new DdgCaptchaError()
70
110
  },
71
111
  { ...retryOverride, shouldRetry: isTransientSearchError, signal },
@@ -74,18 +114,38 @@ export async function ddgSearch(query: string, limit: number, signal?: AbortSign
74
114
  )
75
115
  }
76
116
 
77
- // Both a tripped CAPTCHA and a bare connection timeout are transient: DDG's
78
- // gate lifts after a cooldown, and an egress timeout is almost always a
79
- // momentary network blip (the incident that motivated this: an agent's egress
80
- // timed out for a few minutes, then recovered). Retrying both under the bounded
81
- // `withSearchRetry` backoff turns an otherwise-unbounded model-driven retry
82
- // loop into at most `attempts` code-level tries that then surface a single
83
- // terminal error. A hard network failure (DNS, refused, TLS) is NOT retried —
84
- // it won't fix itself on a re-run and would just burn the budget.
117
+ // Only a bare connection timeout is retried: an egress timeout is almost
118
+ // always a momentary network blip (the incident that motivated this: an agent's
119
+ // egress timed out for a few minutes, then recovered). Retrying under the
120
+ // bounded `withSearchRetry` backoff turns an otherwise-unbounded model-driven
121
+ // retry loop into at most `attempts` code-level tries. A CAPTCHA is NOT retried:
122
+ // DDG's gate lasts minutes, far beyond the retry backoff, and every retry keeps
123
+ // the box alive — the cooldown above handles it instead. A hard network failure
124
+ // (DNS, refused, TLS) is NOT retried either; it won't fix itself on a re-run.
85
125
  export function isTransientSearchError(error: unknown): boolean {
86
- if (error instanceof DdgCaptchaError) return true
87
- if (error instanceof CurlImpersonateError && isCurlExitTimeout(error)) return true
88
- return false
126
+ return error instanceof CurlImpersonateError && isCurlExitTimeout(error)
127
+ }
128
+
129
+ function abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {
130
+ return new Promise((resolve, reject) => {
131
+ if (signal?.aborted) return reject(signal.reason)
132
+ const onAbort = (): void => {
133
+ clearTimeout(timer)
134
+ reject(signal?.reason)
135
+ }
136
+ const timer = setTimeout(() => {
137
+ signal?.removeEventListener('abort', onAbort)
138
+ resolve()
139
+ }, ms)
140
+ signal?.addEventListener('abort', onAbort, { once: true })
141
+ })
142
+ }
143
+
144
+ async function waitForRequestSlot(signal?: AbortSignal): Promise<void> {
145
+ const now = pacing.now()
146
+ const slot = Math.max(now, nextRequestAt)
147
+ nextRequestAt = slot + pacing.intervalMs
148
+ if (slot > now) await pacing.sleep(slot - now, signal)
89
149
  }
90
150
 
91
151
  export class DdgCaptchaError extends Error {
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import { MEMBERSHIP_FRESHNESS_MS, type MembershipCount } from '@/channels/membership'
5
5
  import {
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import { githubReviewChecks } from '@/channels/github-review-check'
5
5
  import {
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import { requestContainerRestart } from '@/agent/restart'
5
5
  import type { RestartHandoffOrigin } from '@/agent/restart-handoff'
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { ChannelRouter } from '@/channels/router'
5
5
 
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from 'node:crypto'
2
2
 
3
- import { Type } from '@earendil-works/pi-ai'
4
3
  import { defineTool } from '@earendil-works/pi-coding-agent'
4
+ import { Type } from 'typebox'
5
5
 
6
6
  import { githubReviewerWorkKey } from '@/channels/github-repo'
7
7
  import { githubReviewChecks, type ReviewerSettlement } from '@/channels/github-review-check'
@@ -122,6 +122,13 @@ export function createSpawnSubagentTool(options: CreateSpawnSubagentToolOptions)
122
122
  'Model profile to run this spawn on, overriding the subagent\'s default tier for this one task. Use "deep" for hard work that needs stronger reasoning (gnarly bugs, non-obvious refactors, failures that resisted a quick fix); omit (or "default") for routine work. Resolves against the configured model profiles; an unknown name falls back to default. Most useful on `operator` — leave it off unless the task clearly warrants a heavier model.',
123
123
  }),
124
124
  ),
125
+ review_skill: Type.Optional(
126
+ Type.String({
127
+ description:
128
+ 'Explicit curated skill for reviewer: code-review, doc-review, plan-review, security-audit, writing-review, data-review, or general. ' +
129
+ 'Valid only for reviewer. Choose only when the review domain is explicit; omit for ambiguous tasks so the reviewer selects dynamically.',
130
+ }),
131
+ ),
125
132
  review_identity: Type.Optional(
126
133
  Type.Object(
127
134
  {
@@ -186,6 +193,10 @@ export function createSpawnSubagentTool(options: CreateSpawnSubagentToolOptions)
186
193
  const payload: Record<string, unknown> = { requestId: taskId, prompt: params.prompt }
187
194
  if (params.description !== undefined) payload.description = params.description
188
195
  if (params.profile !== undefined) payload.profile = params.profile
196
+ if (params.review_skill !== undefined) {
197
+ if (subagentName !== 'reviewer') return errorResult('review_skill is only valid for reviewer subagents')
198
+ payload.skill = params.review_skill
199
+ }
189
200
  if (params.review_identity !== undefined) {
190
201
  if (subagentName !== 'reviewer') {
191
202
  return errorResult('review_identity is only valid for reviewer subagents')
@@ -198,6 +209,9 @@ export function createSpawnSubagentTool(options: CreateSpawnSubagentToolOptions)
198
209
  reviewKind: params.review_identity.review_kind,
199
210
  }
200
211
  }
212
+ if (params.review_skill !== undefined && !subagent.payloadSchema?.safeParse(payload).success) {
213
+ return errorResult('review_skill is not accepted by this reviewer')
214
+ }
201
215
  const workKey =
202
216
  params.review_identity === undefined
203
217
  ? undefined
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import { formatLocalDateTime } from '@/shared'
5
5
  import type { ScanFilter, Stream, StreamMessage, TargetFilter } from '@/stream'
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { PermissionService } from '@/permissions'
5
5
 
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { ChannelRouter } from '@/channels/router'
5
5
  import type { PermissionService } from '@/permissions'
@@ -1,5 +1,5 @@
1
- import { Type } from '@earendil-works/pi-ai'
2
1
  import { defineTool } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
3
 
4
4
  import type { SessionOrigin } from '@/agent/session-origin'
5
5
  import { resolveTodoScope, type TodoScope } from '@/agent/todo/scope'
@@ -1,5 +1,6 @@
1
1
  import { walkHtmlLexically } from './html-lexical'
2
- import { applyReadabilityDetailed, type ReadabilityDetailedResult } from './strategies/readability'
2
+ import type { ReadabilityDetailedResult } from './strategies/readability'
3
+ import { applyReadabilityIsolated } from './strategies/readability-process'
3
4
  import type { HtmlExtractionAttempt } from './types'
4
5
 
5
6
  export type AdaptiveHtmlResult =
@@ -7,7 +8,7 @@ export type AdaptiveHtmlResult =
7
8
  | { kind: 'error'; message: string; attempts: HtmlExtractionAttempt[] }
8
9
 
9
10
  type AdaptiveHtmlDependencies = {
10
- applyReadabilityDetailed: typeof applyReadabilityDetailed
11
+ applyReadabilityDetailed: (html: string, url: string, signal?: AbortSignal) => Promise<ReadabilityDetailedResult>
11
12
  }
12
13
 
13
14
  export const MAX_ADAPTIVE_HTML_SCAN_BYTES = 5 * 1024 * 1024
@@ -31,7 +32,8 @@ const ENCODER = new TextEncoder()
31
32
  export async function extractAdaptiveHtml(
32
33
  html: string,
33
34
  url: string,
34
- dependencies: AdaptiveHtmlDependencies = { applyReadabilityDetailed },
35
+ dependencies: AdaptiveHtmlDependencies = { applyReadabilityDetailed: applyReadabilityIsolated },
36
+ signal?: AbortSignal,
35
37
  ): Promise<AdaptiveHtmlResult> {
36
38
  const attempts: HtmlExtractionAttempt[] = []
37
39
 
@@ -40,7 +42,7 @@ export async function extractAdaptiveHtml(
40
42
  attempts.push({ route: 'readability', outcome: 'error', reason: complexity.reason })
41
43
  } else {
42
44
  try {
43
- const readability = await dependencies.applyReadabilityDetailed(html, url)
45
+ const readability = await dependencies.applyReadabilityDetailed(html, url, signal)
44
46
  const verdict = classifyReadability(readability)
45
47
  if (verdict.useful) {
46
48
  attempts.push({ route: 'readability', outcome: 'success', reason: verdict.reason })
@@ -0,0 +1,25 @@
1
+ import { writeFileSync } from 'node:fs'
2
+
3
+ import { applyReadabilityDetailed } from './readability'
4
+ import type { ReadabilityChildRequest, ReadabilityChildResponse } from './readability-process'
5
+
6
+ // If memory runs out before the parent's watchdog fires, the kernel must pick
7
+ // this disposable parser over the agent runtime. Raising one's own score needs
8
+ // no privilege.
9
+ if (process.platform === 'linux') {
10
+ try {
11
+ writeFileSync('/proc/self/oom_score_adj', '1000')
12
+ } catch (error) {
13
+ console.error(`[webfetch] readability parser could not raise its OOM score: ${String(error)}`)
14
+ }
15
+ }
16
+
17
+ const request = JSON.parse(await Bun.stdin.text()) as ReadabilityChildRequest
18
+ let response: ReadabilityChildResponse
19
+ try {
20
+ response = { ok: true, result: await applyReadabilityDetailed(request.html, request.url) }
21
+ } catch (error) {
22
+ response = { ok: false, message: error instanceof Error ? error.message : String(error) }
23
+ }
24
+ await Bun.write(Bun.stdout, JSON.stringify(response))
25
+ process.exit(0)
@@ -0,0 +1,185 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ import type { ReadabilityDetailedResult } from './readability'
5
+
6
+ export type ReadabilityChildRequest = { html: string; url: string }
7
+ export type ReadabilityChildResponse = { ok: true; result: ReadabilityDetailedResult } | { ok: false; message: string }
8
+
9
+ export type ReadabilityChildOutcome =
10
+ | { kind: 'ok'; result: ReadabilityDetailedResult }
11
+ | { kind: 'error'; message: string }
12
+ | { kind: 'memory'; childRssBytes: number | null; availableBytes: number | null }
13
+ | { kind: 'aborted' }
14
+ | { kind: 'crashed'; exitCode: number | null; signalCode: string | null }
15
+
16
+ type StopReason = Extract<ReadabilityChildOutcome, { kind: 'memory' | 'aborted' }>
17
+
18
+ export type MemoryProbe = {
19
+ readChildRssBytes: (pid: number) => number | null
20
+ readAvailableBytes: () => number | null
21
+ }
22
+
23
+ export type ReadabilityChildOptions = {
24
+ signal?: AbortSignal
25
+ probe?: MemoryProbe
26
+ onSpawn?: (pid: number) => void
27
+ }
28
+
29
+ export const MEMORY_RESERVE_BYTES = 512 * 1024 * 1024
30
+ export const MIN_KILLABLE_CHILD_RSS_BYTES = 256 * 1024 * 1024
31
+ const POLL_INTERVAL_MS = 100
32
+ const CHILD_ENTRY = join(import.meta.dir, 'readability-child.ts')
33
+
34
+ export const READABILITY_MEMORY_MESSAGE =
35
+ 'Readability extraction stopped: parsing this page would exhaust the memory available to the agent. Browser retrieval may be needed for this page.'
36
+ export const READABILITY_CRASH_MESSAGE = 'Readability extraction failed: the page parser exited unexpectedly.'
37
+ export const READABILITY_ABORT_MESSAGE = 'Readability extraction was aborted.'
38
+
39
+ // jsdom can allocate tens of gigabytes inside one synchronous getComputedStyle()
40
+ // call (a 289KB Starlight docs page reached 24-32GB), and memory it frees is not
41
+ // returned to the OS, so every in-process parse ratchets the runtime's RSS. A
42
+ // disposable child process returns all of it on exit and can be killed mid-call,
43
+ // which neither the abort signal nor any in-process limit can do.
44
+ export async function applyReadabilityIsolated(
45
+ html: string,
46
+ url: string,
47
+ signal?: AbortSignal,
48
+ ): Promise<ReadabilityDetailedResult> {
49
+ const outcome = await runReadabilityChild({ html, url }, signal === undefined ? {} : { signal })
50
+ switch (outcome.kind) {
51
+ case 'ok':
52
+ return outcome.result
53
+ case 'error':
54
+ throw new Error(outcome.message)
55
+ case 'memory':
56
+ console.warn(
57
+ `[webfetch] readability parser stopped near memory exhaustion host=${hostOf(url)} child_rss_mb=${toMb(outcome.childRssBytes)} available_mb=${toMb(outcome.availableBytes)}`,
58
+ )
59
+ throw new Error(READABILITY_MEMORY_MESSAGE)
60
+ case 'aborted':
61
+ throw new Error(READABILITY_ABORT_MESSAGE)
62
+ case 'crashed':
63
+ console.warn(
64
+ `[webfetch] readability parser exited unexpectedly host=${hostOf(url)} exit_code=${outcome.exitCode} signal=${outcome.signalCode}`,
65
+ )
66
+ throw new Error(READABILITY_CRASH_MESSAGE)
67
+ }
68
+ }
69
+
70
+ export async function runReadabilityChild(
71
+ request: ReadabilityChildRequest,
72
+ options: ReadabilityChildOptions = {},
73
+ ): Promise<ReadabilityChildOutcome> {
74
+ const { signal } = options
75
+ if (signal?.aborted) return { kind: 'aborted' }
76
+ const probe = options.probe ?? defaultMemoryProbe
77
+
78
+ const child = Bun.spawn([process.execPath, '--smol', CHILD_ENTRY], {
79
+ stdin: new Blob([JSON.stringify(request)]),
80
+ stdout: 'pipe',
81
+ stderr: 'inherit',
82
+ env: {},
83
+ })
84
+
85
+ let stop = null as StopReason | null
86
+ const kill = (reason: StopReason): void => {
87
+ if (stop !== null) return
88
+ stop = reason
89
+ child.kill('SIGKILL')
90
+ }
91
+ const onAbort = (): void => kill({ kind: 'aborted' })
92
+ signal?.addEventListener('abort', onAbort, { once: true })
93
+ const watchdog = setInterval(() => {
94
+ const childRssBytes = probe.readChildRssBytes(child.pid)
95
+ const availableBytes = probe.readAvailableBytes()
96
+ if (shouldStopChild(childRssBytes, availableBytes)) kill({ kind: 'memory', childRssBytes, availableBytes })
97
+ }, POLL_INTERVAL_MS)
98
+ options.onSpawn?.(child.pid)
99
+ if (signal?.aborted) onAbort()
100
+
101
+ try {
102
+ const [stdout] = await Promise.all([new Response(child.stdout).text(), child.exited])
103
+ if (stop !== null) return stop
104
+ if (child.exitCode !== 0) {
105
+ // Anything that SIGKILLs the parser without us asking is the kernel OOM
106
+ // killer: the child raised its own oom_score_adj so it is chosen first.
107
+ if (child.signalCode === 'SIGKILL') return { kind: 'memory', childRssBytes: null, availableBytes: null }
108
+ return { kind: 'crashed', exitCode: child.exitCode, signalCode: child.signalCode }
109
+ }
110
+ const response = parseResponse(stdout)
111
+ if (response === null) return { kind: 'crashed', exitCode: child.exitCode, signalCode: child.signalCode }
112
+ return response.ok ? { kind: 'ok', result: response.result } : { kind: 'error', message: response.message }
113
+ } finally {
114
+ clearInterval(watchdog)
115
+ signal?.removeEventListener('abort', onAbort)
116
+ }
117
+ }
118
+
119
+ // Stopping only when the machine is about to run out keeps every parse that
120
+ // succeeds today succeeding: a parse is cut short only where it would otherwise
121
+ // have taken the agent runtime down with it. The RSS floor keeps a normal-sized
122
+ // parse alive when something else is what is exhausting memory.
123
+ export function shouldStopChild(childRssBytes: number | null, availableBytes: number | null): boolean {
124
+ if (childRssBytes === null || availableBytes === null) return false
125
+ return childRssBytes >= MIN_KILLABLE_CHILD_RSS_BYTES && availableBytes < MEMORY_RESERVE_BYTES
126
+ }
127
+
128
+ export const defaultMemoryProbe: MemoryProbe = {
129
+ readChildRssBytes: (pid) => {
130
+ if (process.platform !== 'linux') return null
131
+ const status = readOptional(`/proc/${pid}/status`)
132
+ const kb = status?.match(/^VmRSS:\s+(\d+)\s+kB$/m)?.[1]
133
+ return kb === undefined ? null : Number(kb) * 1024
134
+ },
135
+ readAvailableBytes: () => (process.platform === 'linux' ? readLinuxAvailableBytes() : null),
136
+ }
137
+
138
+ // The tighter of two ceilings: the container's cgroup limit, and the host or VM
139
+ // itself. Both matter because sibling containers can exhaust a shared Docker VM
140
+ // while every container is still under its own limit, which is how this OOM
141
+ // actually happened.
142
+ export function readLinuxAvailableBytes(root = ''): number | null {
143
+ const candidates: number[] = []
144
+ const meminfoKb = readOptional(`${root}/proc/meminfo`)?.match(/^MemAvailable:\s+(\d+)\s+kB$/m)?.[1]
145
+ if (meminfoKb !== undefined) candidates.push(Number(meminfoKb) * 1024)
146
+
147
+ const max = readOptional(`${root}/sys/fs/cgroup/memory.max`)?.trim()
148
+ const current = readOptional(`${root}/sys/fs/cgroup/memory.current`)?.trim()
149
+ if (max !== undefined && max !== 'max' && current !== undefined) {
150
+ // Inactive page cache is reclaimable before the cgroup OOMs, the same
151
+ // adjustment `docker stats` applies.
152
+ const inactiveFile = readOptional(`${root}/sys/fs/cgroup/memory.stat`)?.match(/^inactive_file (\d+)$/m)?.[1]
153
+ candidates.push(Number(max) - Number(current) + Number(inactiveFile ?? 0))
154
+ }
155
+
156
+ return candidates.length === 0 ? null : Math.min(...candidates)
157
+ }
158
+
159
+ function parseResponse(stdout: string): ReadabilityChildResponse | null {
160
+ try {
161
+ return JSON.parse(stdout) as ReadabilityChildResponse
162
+ } catch {
163
+ return null
164
+ }
165
+ }
166
+
167
+ function readOptional(path: string): string | undefined {
168
+ try {
169
+ return readFileSync(path, 'utf8')
170
+ } catch {
171
+ return undefined
172
+ }
173
+ }
174
+
175
+ function hostOf(url: string): string {
176
+ try {
177
+ return new URL(url).host
178
+ } catch {
179
+ return 'invalid-url'
180
+ }
181
+ }
182
+
183
+ function toMb(bytes: number | null): string {
184
+ return bytes === null ? '?' : String(Math.round(bytes / (1024 * 1024)))
185
+ }