telecoder 0.0.0-stage → 1.3.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 (235) hide show
  1. package/.env.example +375 -0
  2. package/CHANGELOG.md +1867 -0
  3. package/LICENSE +22 -0
  4. package/README.md +130 -2
  5. package/dist/audio/transcribe.js +41 -0
  6. package/dist/bin/mcp-server.js +363 -0
  7. package/dist/bin/tool-result.js +22 -0
  8. package/dist/bot/access/group-access.js +147 -0
  9. package/dist/bot/bot.js +573 -0
  10. package/dist/bot/command-catalog.js +263 -0
  11. package/dist/bot/deleted-threads.js +103 -0
  12. package/dist/bot/file-access.js +150 -0
  13. package/dist/bot/handlers/album.handler.js +128 -0
  14. package/dist/bot/handlers/branch.handler.js +1096 -0
  15. package/dist/bot/handlers/command/access.js +407 -0
  16. package/dist/bot/handlers/command/admin.js +803 -0
  17. package/dist/bot/handlers/command/all-view.js +33 -0
  18. package/dist/bot/handlers/command/auto-approve.js +138 -0
  19. package/dist/bot/handlers/command/background.js +37 -0
  20. package/dist/bot/handlers/command/botname.js +76 -0
  21. package/dist/bot/handlers/command/btw.js +175 -0
  22. package/dist/bot/handlers/command/cleanup.js +329 -0
  23. package/dist/bot/handlers/command/model.js +226 -0
  24. package/dist/bot/handlers/command/prefs-scope.js +362 -0
  25. package/dist/bot/handlers/command/project.js +458 -0
  26. package/dist/bot/handlers/command/resume.js +325 -0
  27. package/dist/bot/handlers/command/session.js +299 -0
  28. package/dist/bot/handlers/command/settings.js +657 -0
  29. package/dist/bot/handlers/command/shared.js +207 -0
  30. package/dist/bot/handlers/command/tasks.js +681 -0
  31. package/dist/bot/handlers/command/thread.js +128 -0
  32. package/dist/bot/handlers/command/topic-store.js +74 -0
  33. package/dist/bot/handlers/command/topic.js +27 -0
  34. package/dist/bot/handlers/command/usage.js +382 -0
  35. package/dist/bot/handlers/command-fallback.js +179 -0
  36. package/dist/bot/handlers/command.handler.js +446 -0
  37. package/dist/bot/handlers/loop.handler.js +389 -0
  38. package/dist/bot/handlers/media-fallback.js +126 -0
  39. package/dist/bot/handlers/message/replies.js +56 -0
  40. package/dist/bot/handlers/message/shared.js +95 -0
  41. package/dist/bot/handlers/message/turn-notify.js +135 -0
  42. package/dist/bot/handlers/message/turn-runner.js +56 -0
  43. package/dist/bot/handlers/message.handler.js +114 -0
  44. package/dist/bot/handlers/newbot.handler.js +230 -0
  45. package/dist/bot/handlers/photo.handler.js +286 -0
  46. package/dist/bot/handlers/prompts.handler.js +343 -0
  47. package/dist/bot/handlers/running-note.js +44 -0
  48. package/dist/bot/handlers/session-guard.js +78 -0
  49. package/dist/bot/handlers/suggestion.handler.js +84 -0
  50. package/dist/bot/handlers/tree.handler.js +480 -0
  51. package/dist/bot/handlers/view.js +41 -0
  52. package/dist/bot/handlers/voice.handler.js +147 -0
  53. package/dist/bot/kept-threads.js +51 -0
  54. package/dist/bot/middleware/admin-only.js +49 -0
  55. package/dist/bot/middleware/album-collector.js +69 -0
  56. package/dist/bot/middleware/auth.middleware.js +122 -0
  57. package/dist/bot/middleware/group-mention.middleware.js +145 -0
  58. package/dist/bot/middleware/group-role.middleware.js +75 -0
  59. package/dist/bot/middleware/message-batcher.js +87 -0
  60. package/dist/bot/middleware/queued-messages.js +1082 -0
  61. package/dist/bot/middleware/stale-filter.js +9 -0
  62. package/dist/build-info.json +8 -0
  63. package/dist/claude/ask-user-form.js +517 -0
  64. package/dist/claude/ask-user.js +363 -0
  65. package/dist/claude/auto-approve.js +225 -0
  66. package/dist/claude/auto-topic-haiku.js +143 -0
  67. package/dist/claude/available-commands.js +156 -0
  68. package/dist/claude/background-hint.js +91 -0
  69. package/dist/claude/branch-tree.js +393 -0
  70. package/dist/claude/carry-over.js +25 -0
  71. package/dist/claude/ccr-shim.js +48 -0
  72. package/dist/claude/charter-judge.js +214 -0
  73. package/dist/claude/claude-auth.js +117 -0
  74. package/dist/claude/command-parser.js +206 -0
  75. package/dist/claude/cost-overlay-scraper.js +134 -0
  76. package/dist/claude/effort-level.js +5 -0
  77. package/dist/claude/enabled-plugins.js +52 -0
  78. package/dist/claude/in-flight-tracker.js +82 -0
  79. package/dist/claude/ipc-server.js +192 -0
  80. package/dist/claude/mcp-bridge.js +481 -0
  81. package/dist/claude/memory-guard.js +400 -0
  82. package/dist/claude/message-offsets.js +88 -0
  83. package/dist/claude/modal-relay.js +817 -0
  84. package/dist/claude/model-catalog.js +102 -0
  85. package/dist/claude/monitor-relay.js +712 -0
  86. package/dist/claude/native-commands.js +40 -0
  87. package/dist/claude/pending-branches.js +93 -0
  88. package/dist/claude/pending-suggestions.js +50 -0
  89. package/dist/claude/permission-gate.js +338 -0
  90. package/dist/claude/permission-mode.js +152 -0
  91. package/dist/claude/poll-user.js +51 -0
  92. package/dist/claude/prewarm.js +70 -0
  93. package/dist/claude/project-commands.js +69 -0
  94. package/dist/claude/prompt-hold.js +124 -0
  95. package/dist/claude/prompt-suggestion-scraper.js +89 -0
  96. package/dist/claude/pty-idle-reaper.js +96 -0
  97. package/dist/claude/pty-provider.js +2499 -0
  98. package/dist/claude/pty-spawn-config.js +343 -0
  99. package/dist/claude/pty-stream.js +363 -0
  100. package/dist/claude/pty-watch.js +201 -0
  101. package/dist/claude/request-queue.js +107 -0
  102. package/dist/claude/rewind.js +111 -0
  103. package/dist/claude/scheduled-runner.js +89 -0
  104. package/dist/claude/scheduler.js +241 -0
  105. package/dist/claude/scope-guard.js +279 -0
  106. package/dist/claude/screen-marks.js +83 -0
  107. package/dist/claude/screen-render.js +96 -0
  108. package/dist/claude/secret-request.js +576 -0
  109. package/dist/claude/session-history.js +288 -0
  110. package/dist/claude/session-jsonl.js +1067 -0
  111. package/dist/claude/session-manager.js +216 -0
  112. package/dist/claude/session-move.js +70 -0
  113. package/dist/claude/session-scan.js +270 -0
  114. package/dist/claude/setup-token.js +285 -0
  115. package/dist/claude/shell-mode-gate.js +60 -0
  116. package/dist/claude/side-call-options.js +45 -0
  117. package/dist/claude/side-question.js +252 -0
  118. package/dist/claude/stage-images.js +62 -0
  119. package/dist/claude/subagent-tools.js +12 -0
  120. package/dist/claude/synthetic-ctx.js +51 -0
  121. package/dist/claude/tasks-overlay-scraper.js +217 -0
  122. package/dist/claude/thread-judge-haiku.js +88 -0
  123. package/dist/claude/tip-scraper.js +77 -0
  124. package/dist/claude/tui-modal.js +746 -0
  125. package/dist/claude/tui-state.js +456 -0
  126. package/dist/claude/turn-end.js +105 -0
  127. package/dist/claude/turn-prose.js +64 -0
  128. package/dist/claude/typed-answer.js +104 -0
  129. package/dist/claude/update-banner-relay.js +70 -0
  130. package/dist/cli/commands/doctor.js +338 -0
  131. package/dist/cli/commands/help.js +34 -0
  132. package/dist/cli/commands/init.js +126 -0
  133. package/dist/cli/commands/service.js +710 -0
  134. package/dist/cli/commands/start.js +63 -0
  135. package/dist/cli/commands/version.js +11 -0
  136. package/dist/cli/commands/watch.js +322 -0
  137. package/dist/cli/dispatch.js +84 -0
  138. package/dist/cli/init/env-file.js +99 -0
  139. package/dist/cli/init/flow.js +705 -0
  140. package/dist/cli/init/prompter.js +124 -0
  141. package/dist/cli/init/telegram.js +120 -0
  142. package/dist/cli/service/render.js +212 -0
  143. package/dist/cli.js +18 -0
  144. package/dist/config-report.js +107 -0
  145. package/dist/config.js +525 -0
  146. package/dist/index.js +443 -0
  147. package/dist/launcher.js +960 -0
  148. package/dist/node-version-check.js +27 -0
  149. package/dist/providers/ccr-health.js +119 -0
  150. package/dist/providers/ccr-introspect.js +142 -0
  151. package/dist/providers/ccr-provider.js +59 -0
  152. package/dist/providers/claude-provider.js +10 -0
  153. package/dist/providers/prefs-sync.js +121 -0
  154. package/dist/providers/project-favorites.js +74 -0
  155. package/dist/providers/provider-router.js +128 -0
  156. package/dist/providers/provider-switch.js +91 -0
  157. package/dist/providers/spawn-settings.js +45 -0
  158. package/dist/providers/types.js +8 -0
  159. package/dist/providers/user-preferences.js +322 -0
  160. package/dist/restart-when-idle.js +173 -0
  161. package/dist/startup/auto-continue.js +67 -0
  162. package/dist/startup/auto-resume.js +161 -0
  163. package/dist/startup/notices.js +120 -0
  164. package/dist/startup/shared.js +44 -0
  165. package/dist/startup/verify-token.js +31 -0
  166. package/dist/telegram/access-request.js +153 -0
  167. package/dist/telegram/action-logger.js +522 -0
  168. package/dist/telegram/admin-mention.js +107 -0
  169. package/dist/telegram/background-activity-card.js +502 -0
  170. package/dist/telegram/botname-settings.js +282 -0
  171. package/dist/telegram/button-text.js +26 -0
  172. package/dist/telegram/client-options.js +26 -0
  173. package/dist/telegram/deduplication.js +33 -0
  174. package/dist/telegram/entities.js +295 -0
  175. package/dist/telegram/force-reply-tracker.js +96 -0
  176. package/dist/telegram/lettered-options.js +87 -0
  177. package/dist/telegram/markdown.js +362 -0
  178. package/dist/telegram/message-sender.js +1019 -0
  179. package/dist/telegram/prompt-label.js +148 -0
  180. package/dist/telegram/prompt-suggestion.js +54 -0
  181. package/dist/telegram/send.js +123 -0
  182. package/dist/telegram/streaming-turn.js +103 -0
  183. package/dist/telegram/suggestions-settings.js +30 -0
  184. package/dist/telegram/table-lists.js +56 -0
  185. package/dist/telegram/task-list.js +367 -0
  186. package/dist/telegram/task-tracker.js +128 -0
  187. package/dist/telegram/terminal-renderer.js +667 -0
  188. package/dist/telegram/thread-titles.js +262 -0
  189. package/dist/telegram/threads-setup.js +117 -0
  190. package/dist/utils/admins.js +143 -0
  191. package/dist/utils/agent-timer.js +55 -0
  192. package/dist/utils/atomic-write.js +24 -0
  193. package/dist/utils/bot-icon.js +248 -0
  194. package/dist/utils/bot-token.js +77 -0
  195. package/dist/utils/bounded-map.js +55 -0
  196. package/dist/utils/build-info.js +57 -0
  197. package/dist/utils/caffeinate.js +53 -0
  198. package/dist/utils/claude-env.js +53 -0
  199. package/dist/utils/claude-version.js +48 -0
  200. package/dist/utils/config-home.js +62 -0
  201. package/dist/utils/download.js +137 -0
  202. package/dist/utils/exit-codes.js +59 -0
  203. package/dist/utils/file-type.js +149 -0
  204. package/dist/utils/format.js +78 -0
  205. package/dist/utils/home-project.js +30 -0
  206. package/dist/utils/host-stall.js +51 -0
  207. package/dist/utils/id-list.js +44 -0
  208. package/dist/utils/idle-restart.js +92 -0
  209. package/dist/utils/install-mode.js +63 -0
  210. package/dist/utils/instance-config.js +148 -0
  211. package/dist/utils/instances.js +95 -0
  212. package/dist/utils/json-store.js +140 -0
  213. package/dist/utils/keyed-settings.js +62 -0
  214. package/dist/utils/legacy-env.js +35 -0
  215. package/dist/utils/memory-pressure.js +60 -0
  216. package/dist/utils/npm-version.js +134 -0
  217. package/dist/utils/prefs-relay.js +107 -0
  218. package/dist/utils/proc-children.js +102 -0
  219. package/dist/utils/resolve-bin.js +86 -0
  220. package/dist/utils/resolve-claude-bin.js +52 -0
  221. package/dist/utils/respawn-backoff.js +28 -0
  222. package/dist/utils/sanitize.js +91 -0
  223. package/dist/utils/session-key.js +37 -0
  224. package/dist/utils/stale-launcher.js +109 -0
  225. package/dist/utils/state-dir-migration.js +92 -0
  226. package/dist/utils/systemd.js +126 -0
  227. package/dist/utils/telegram-proxy.js +72 -0
  228. package/dist/utils/uploads-dir.js +27 -0
  229. package/dist/utils/user-roster.js +275 -0
  230. package/dist/utils/verbosity.js +118 -0
  231. package/dist/utils/version-check.js +166 -0
  232. package/dist/utils/workspace-guard.js +44 -0
  233. package/dist/worker-restart.js +141 -0
  234. package/instances.json.example +41 -0
  235. package/package.json +75 -4
@@ -0,0 +1,279 @@
1
+ /**
2
+ * Does this tool call reach outside the projects the bot was shared for?
3
+ *
4
+ * The permission gate's pattern list answers "is this command destructive".
5
+ * It has nothing to say about `cat ~/.ssh/id_rsa`, or an edit to a project
6
+ * nobody was invited to, because neither is destructive — they are simply out
7
+ * of bounds. This module answers that second question.
8
+ *
9
+ * It is a supervision layer, not a sandbox. Claude still runs as the bot's Unix
10
+ * user with permissions bypassed, so anything here can be defeated by a command
11
+ * written to avoid naming its path. The point is that the ordinary way to reach
12
+ * out of scope — naming the path — surfaces as an approval prompt, and the
13
+ * ordinary way to work inside scope never does.
14
+ *
15
+ * That balance is why the rules below are shaped the way they are. A guest's
16
+ * session reads from `/usr`, `/etc` and the language toolchains all day; making
17
+ * those prompt would train everyone to tap Allow without reading. So system
18
+ * paths are readable, credential directories never are, and everything else is
19
+ * judged against the configured roots.
20
+ */
21
+ import * as os from 'os';
22
+ import * as path from 'path';
23
+ import { config } from '../config.js';
24
+ import { getWorkspaceRoot } from '../utils/workspace-guard.js';
25
+ import { getUploadsRoot } from '../utils/uploads-dir.js';
26
+ /** Tools whose input names a path we can check directly. */
27
+ const PATH_FIELDS = {
28
+ Read: ['file_path'],
29
+ Write: ['file_path'],
30
+ Edit: ['file_path'],
31
+ NotebookEdit: ['notebook_path'],
32
+ Glob: ['path'],
33
+ Grep: ['path'],
34
+ };
35
+ /**
36
+ * Directories whose contents are credentials, and the bot's own state. Always
37
+ * prompt, even when they sit inside an allowed root — which they do the moment
38
+ * someone points WORKSPACE_DIR at a home directory.
39
+ *
40
+ * Matched on path segments, so `~/.ssh` catches `/home/x/.ssh/id_rsa` without
41
+ * also catching `/srv/app/ssh-helper`.
42
+ */
43
+ const SENSITIVE_SEGMENTS = [
44
+ '.ssh',
45
+ '.aws',
46
+ '.gnupg',
47
+ '.kube',
48
+ '.docker',
49
+ '.claude',
50
+ '.claudegram',
51
+ '.telecoder',
52
+ '.password-store',
53
+ '.mozilla',
54
+ '.thunderbird',
55
+ ];
56
+ /** Credential files, matched on basename. */
57
+ const SENSITIVE_FILES = [
58
+ '.netrc',
59
+ '.npmrc',
60
+ '.pypirc',
61
+ '.git-credentials',
62
+ '.pgpass',
63
+ 'id_rsa',
64
+ 'id_dsa',
65
+ 'id_ecdsa',
66
+ 'id_ed25519',
67
+ 'shadow',
68
+ 'sudoers',
69
+ ];
70
+ /** `~/.config/<name>` holding credentials rather than preferences. */
71
+ const SENSITIVE_CONFIG_SUBDIRS = ['gcloud', 'gh', 'op', 'doctl', 'fly', 'rclone'];
72
+ /**
73
+ * Readable without a prompt: the operating system and the language toolchains.
74
+ * Reading these is background noise in any real session, and a prompt for each
75
+ * one would be a prompt nobody reads.
76
+ */
77
+ const SYSTEM_READ_PREFIXES = [
78
+ '/usr', '/etc', '/opt', '/bin', '/sbin', '/lib', '/lib64',
79
+ '/proc', '/sys', '/var/log', '/var/lib', '/snap', '/nix',
80
+ '/home/linuxbrew', '/Library', '/System', '/Applications',
81
+ ];
82
+ /** Same idea, under the bot user's home. Deliberately excludes `.config`. */
83
+ const HOME_READ_SUFFIXES = [
84
+ '.nvm', '.npm', '.cargo', '.rustup', '.pyenv', '.rbenv', '.sdkman',
85
+ '.cache', '.local/lib', '.local/bin', '.bun', '.deno',
86
+ ];
87
+ /** True when the scope guard runs at all. Follows the gate unless overridden. */
88
+ export function isScopeGuardEnabled(gateEnabled) {
89
+ if (config.SCOPE_GUARD === 'on')
90
+ return true;
91
+ if (config.SCOPE_GUARD === 'off')
92
+ return false;
93
+ return gateEnabled;
94
+ }
95
+ /**
96
+ * The directories a session may work in without asking. `/tmp` is included
97
+ * because every toolchain writes there and nothing durable lives there.
98
+ */
99
+ export function getAllowedRoots() {
100
+ const roots = [getWorkspaceRoot(), os.tmpdir(), '/tmp', getUploadsRoot(), ...config.SCOPE_ALLOWED_PATHS];
101
+ return dedupe(roots.map((r) => path.resolve(r)));
102
+ }
103
+ /**
104
+ * Inspect a tool call for paths outside the allowed roots.
105
+ *
106
+ * `cwd` is the session's working directory, used to resolve relative paths —
107
+ * without it a `../../..` climb reads as harmless.
108
+ */
109
+ export function checkToolScope(toolName, toolInput, cwd) {
110
+ const roots = getAllowedRoots();
111
+ const fields = PATH_FIELDS[toolName];
112
+ if (fields) {
113
+ for (const field of fields) {
114
+ const raw = toolInput[field];
115
+ if (typeof raw !== 'string' || !raw)
116
+ continue;
117
+ const verdict = classify(raw, cwd, roots);
118
+ if (verdict.outOfScope)
119
+ return verdict;
120
+ }
121
+ return { outOfScope: false };
122
+ }
123
+ if (toolName === 'Bash') {
124
+ const command = typeof toolInput.command === 'string' ? toolInput.command : '';
125
+ for (const token of extractPathTokens(command)) {
126
+ const verdict = classify(token, cwd, roots);
127
+ if (verdict.outOfScope)
128
+ return verdict;
129
+ }
130
+ }
131
+ return { outOfScope: false };
132
+ }
133
+ /** Judge one path string against the roots. Exported for tests. */
134
+ export function classify(raw, cwd, roots) {
135
+ const resolved = resolvePath(raw, cwd);
136
+ if (!resolved)
137
+ return { outOfScope: false };
138
+ if (isSensitive(resolved)) {
139
+ return {
140
+ outOfScope: true,
141
+ reason: 'reads a credential or bot-state path',
142
+ offender: display(resolved),
143
+ };
144
+ }
145
+ if (roots.some((root) => within(root, resolved)))
146
+ return { outOfScope: false };
147
+ if (isSystemReadable(resolved))
148
+ return { outOfScope: false };
149
+ return {
150
+ outOfScope: true,
151
+ reason: 'path outside the shared projects',
152
+ offender: display(resolved),
153
+ };
154
+ }
155
+ /**
156
+ * Turn a raw path string into an absolute one, or undefined when it isn't a
157
+ * path we can judge. Only strings that announce themselves as paths qualify —
158
+ * a leading `/`, `~`, `$HOME`, `./` or `../` — which is what keeps `sed`'s
159
+ * `s/a/b/` and a bare `npm` out of the results.
160
+ */
161
+ export function resolvePath(raw, cwd) {
162
+ let value = raw.trim().replace(/^['"]+|['"]+$/g, '');
163
+ if (!value)
164
+ return undefined;
165
+ // Home expansion runs before the glob trim, not after: `${HOME}` is braces
166
+ // around a variable, and trimming at the first brace would leave a bare `$`.
167
+ value = value.replace(/^\$\{HOME\}/, os.homedir()).replace(/^\$HOME\b/, os.homedir());
168
+ if (value === '~')
169
+ value = os.homedir();
170
+ else if (value.startsWith('~/'))
171
+ value = path.join(os.homedir(), value.slice(2));
172
+ // Glob and brace metacharacters: keep the literal prefix, which is what
173
+ // decides containment.
174
+ const metaIdx = value.search(/[*?[\]{}]/);
175
+ if (metaIdx === 0)
176
+ return undefined;
177
+ if (metaIdx > 0)
178
+ value = value.slice(0, metaIdx);
179
+ if (!value)
180
+ return undefined;
181
+ if (value.startsWith('/'))
182
+ return path.resolve(value);
183
+ if (value.startsWith('./') || value.startsWith('../') || value === '..') {
184
+ return path.resolve(cwd, value);
185
+ }
186
+ return undefined;
187
+ }
188
+ /**
189
+ * Pull path-looking tokens out of a shell command.
190
+ *
191
+ * Approximate by nature, and deliberately biased toward missing things rather
192
+ * than inventing them: a false positive is a prompt for a command that was
193
+ * fine, which is the cost that makes people stop reading prompts.
194
+ */
195
+ export function extractPathTokens(command) {
196
+ const tokens = [];
197
+ for (const rawToken of command.split(/[\s;|&()<>]+/)) {
198
+ if (!rawToken)
199
+ continue;
200
+ // A URL is not a filesystem path; `//` after a scheme would resolve to one.
201
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(rawToken))
202
+ continue;
203
+ // `--config=/path` and `-I/usr/include` carry a path after the flag.
204
+ const candidates = [rawToken];
205
+ const eq = rawToken.indexOf('=');
206
+ if (eq > 0)
207
+ candidates.push(rawToken.slice(eq + 1));
208
+ if (/^-[A-Za-z]/.test(rawToken))
209
+ candidates.push(rawToken.replace(/^-+[A-Za-z]*/, ''));
210
+ for (const candidate of candidates) {
211
+ const stripped = candidate.replace(/^['"]+/, '');
212
+ if (stripped.startsWith('/') ||
213
+ stripped.startsWith('~') ||
214
+ stripped.startsWith('$HOME') ||
215
+ stripped.startsWith('${HOME}') ||
216
+ stripped.startsWith('./') ||
217
+ stripped.startsWith('../')) {
218
+ tokens.push(stripped);
219
+ }
220
+ }
221
+ }
222
+ return dedupe(tokens);
223
+ }
224
+ /**
225
+ * Is this absolute path a credential or bot-state path? The same denylist the
226
+ * tool-call check uses, for the other ways a file leaves the machine — `/file`
227
+ * and the send-file tool — so there is one list to keep current.
228
+ */
229
+ export function isSensitivePath(absolutePath) {
230
+ return isSensitive(path.resolve(absolutePath));
231
+ }
232
+ function isSensitive(resolved) {
233
+ // The one corner of the bot's state that holds the user's own files: what
234
+ // they sent in the chat, saved there for Claude to read (utils/uploads-dir.ts).
235
+ if (within(getUploadsRoot(), resolved))
236
+ return false;
237
+ const segments = resolved.split(path.sep).filter(Boolean);
238
+ if (segments.some((s) => SENSITIVE_SEGMENTS.includes(s)))
239
+ return true;
240
+ const base = segments[segments.length - 1];
241
+ if (base && SENSITIVE_FILES.includes(base))
242
+ return true;
243
+ const configIdx = segments.indexOf('.config');
244
+ if (configIdx >= 0) {
245
+ const next = segments[configIdx + 1];
246
+ if (next && SENSITIVE_CONFIG_SUBDIRS.includes(next))
247
+ return true;
248
+ }
249
+ // Any dotenv outside the allowed roots — the bot's own token file is one.
250
+ if (base === '.env' || base?.startsWith('.env.'))
251
+ return true;
252
+ return false;
253
+ }
254
+ function isSystemReadable(resolved) {
255
+ if (SYSTEM_READ_PREFIXES.some((prefix) => within(prefix, resolved)))
256
+ return true;
257
+ const home = os.homedir();
258
+ return HOME_READ_SUFFIXES.some((suffix) => within(path.join(home, suffix), resolved));
259
+ }
260
+ /**
261
+ * Containment by path segment. Purely lexical on already-resolved paths: unlike
262
+ * utils/workspace-guard this must judge paths that do not exist yet, which is
263
+ * most of them at PreToolUse time.
264
+ */
265
+ function within(root, target) {
266
+ const r = path.resolve(root);
267
+ return target === r || target.startsWith(r.endsWith(path.sep) ? r : r + path.sep);
268
+ }
269
+ /** Shorten a home path for display, matching how the rest of the bot logs. */
270
+ function display(resolved) {
271
+ const home = os.homedir();
272
+ return resolved === home || resolved.startsWith(home + path.sep)
273
+ ? '~' + resolved.slice(home.length)
274
+ : resolved;
275
+ }
276
+ function dedupe(values) {
277
+ return [...new Set(values)];
278
+ }
279
+ //# sourceMappingURL=scope-guard.js.map
@@ -0,0 +1,83 @@
1
+ /**
2
+ * How the screen is drawn, for the two things Claude Code says in colour only.
3
+ *
4
+ * Which tab of a dialog is open: `/permissions` and `/config` print every
5
+ * tab's name in one row and fill the open one's background. And which list
6
+ * holds the cursor: `/memory` has two, each with a `❯` of its own, and
7
+ * `/config` keeps its `❯` on screen while the keys go to the search box above
8
+ * it. The glyph that isn't taking keys is drawn grey. As plain text, each of
9
+ * those pairs reads the same.
10
+ *
11
+ * So the screen gets a second reading, the same shape as its text: one mark
12
+ * for every character, row for row, saying only what the dialog parser needs
13
+ * to know about it. Colours themselves stay out of it. Which blue a theme
14
+ * fills a tab with is no business of the parser's, and a test can write a
15
+ * row of marks by hand.
16
+ */
17
+ /** Drawn on a filled background, or in reverse video: the open tab. */
18
+ export const FILLED = '#';
19
+ /** Drawn grey or dim: a cursor glyph whose list isn't taking keys. */
20
+ export const MUTED = '.';
21
+ /** Anything else. */
22
+ export const PLAIN = ' ';
23
+ /** The 256-colour palette's grey ramp, and the two greys of the 16 before it. */
24
+ function greyInPalette(index) {
25
+ return index === 7 || index === 8 || (index >= 232 && index <= 255);
26
+ }
27
+ /** How far apart an RGB grey's channels may sit. */
28
+ const GREY_SPREAD = 12;
29
+ function greyRgb(color) {
30
+ const channels = [(color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff];
31
+ return Math.max(...channels) - Math.min(...channels) <= GREY_SPREAD;
32
+ }
33
+ function markFor(cell) {
34
+ if (cell.isInverse() || cell.isBgRGB() || cell.isBgPalette())
35
+ return FILLED;
36
+ if (cell.isDim())
37
+ return MUTED;
38
+ if (cell.isFgPalette() && greyInPalette(cell.getFgColor()))
39
+ return MUTED;
40
+ if (cell.isFgRGB() && greyRgb(cell.getFgColor()))
41
+ return MUTED;
42
+ return PLAIN;
43
+ }
44
+ /**
45
+ * The marks for every row of the terminal's buffer, scrollback included,
46
+ * joined by newlines: row `n` here is row `n` of the buffer read as text with
47
+ * `translateToString`, and character `k` of one is character `k` of the
48
+ * other. A row of marks runs the terminal's full width, so it is as long as
49
+ * its text or longer.
50
+ *
51
+ * Only the rows on screen are read. A dialog is drawn there and nowhere else,
52
+ * and the scrollback above can be thousands of rows, read cell by cell on
53
+ * every poll of a relay waiting for a key to land. Its rows come back empty,
54
+ * which reads as "nothing marked".
55
+ */
56
+ export function screenMarks(term) {
57
+ const buffer = term.buffer.active;
58
+ const rows = [];
59
+ let cell;
60
+ const onScreen = Math.max(0, buffer.length - term.rows);
61
+ for (let y = 0; y < buffer.length; y++) {
62
+ if (y < onScreen) {
63
+ rows.push('');
64
+ continue;
65
+ }
66
+ const line = buffer.getLine(y);
67
+ let row = '';
68
+ for (let x = 0; x < term.cols; x++) {
69
+ cell = line?.getCell(x, cell);
70
+ if (!cell) {
71
+ row += PLAIN;
72
+ continue;
73
+ }
74
+ // The second half of a wide character adds nothing to the text either.
75
+ if (cell.getWidth() === 0)
76
+ continue;
77
+ row += markFor(cell).repeat((cell.getChars() || ' ').length);
78
+ }
79
+ rows.push(row);
80
+ }
81
+ return rows.join('\n');
82
+ }
83
+ //# sourceMappingURL=screen-marks.js.map
@@ -0,0 +1,96 @@
1
+ /**
2
+ * A headless terminal's visible screen, as text another terminal can show.
3
+ *
4
+ * `telecoder watch` uses it on both ends: the bot turns a claude's screen into
5
+ * a snapshot for a watcher that has just joined, and the watcher paints its own
6
+ * copy of the screen row by row. Each row is written out cell by cell with its
7
+ * colours, so nothing depends on the watching terminal being the same size as
8
+ * claude's: a row never relies on wrapping or scrolling to land where it should.
9
+ */
10
+ const RESET = '\x1b[0m';
11
+ /** The SGR parameters for one cell's look; '' for plain text. */
12
+ function cellStyle(cell) {
13
+ const sgr = [];
14
+ if (cell.isBold())
15
+ sgr.push('1');
16
+ if (cell.isDim())
17
+ sgr.push('2');
18
+ if (cell.isItalic())
19
+ sgr.push('3');
20
+ if (cell.isUnderline())
21
+ sgr.push('4');
22
+ if (cell.isInverse())
23
+ sgr.push('7');
24
+ if (cell.isInvisible())
25
+ sgr.push('8');
26
+ if (cell.isStrikethrough())
27
+ sgr.push('9');
28
+ if (cell.isFgRGB()) {
29
+ const c = cell.getFgColor();
30
+ sgr.push(`38;2;${(c >> 16) & 0xff};${(c >> 8) & 0xff};${c & 0xff}`);
31
+ }
32
+ else if (cell.isFgPalette()) {
33
+ sgr.push(`38;5;${cell.getFgColor()}`);
34
+ }
35
+ if (cell.isBgRGB()) {
36
+ const c = cell.getBgColor();
37
+ sgr.push(`48;2;${(c >> 16) & 0xff};${(c >> 8) & 0xff};${c & 0xff}`);
38
+ }
39
+ else if (cell.isBgPalette()) {
40
+ sgr.push(`48;5;${cell.getBgColor()}`);
41
+ }
42
+ return sgr.join(';');
43
+ }
44
+ /**
45
+ * The visible rows, top to bottom, each exactly as wide as the screen (or
46
+ * `maxCols`), ending with its colours reset.
47
+ */
48
+ export function renderRows(term, { maxCols } = {}) {
49
+ const buffer = term.buffer.active;
50
+ const cols = Math.min(term.cols, maxCols ?? term.cols);
51
+ const rows = [];
52
+ for (let y = 0; y < term.rows; y++) {
53
+ const line = buffer.getLine(buffer.baseY + y);
54
+ let out = '';
55
+ let style = '';
56
+ let cell;
57
+ for (let x = 0; x < cols; x++) {
58
+ cell = line?.getCell(x, cell);
59
+ if (!cell) {
60
+ out += ' ';
61
+ continue;
62
+ }
63
+ const width = cell.getWidth();
64
+ // The second half of a wide character: its first half already drew it.
65
+ if (width === 0)
66
+ continue;
67
+ // A wide character that would stick out past the last column.
68
+ if (width === 2 && x + 1 >= cols) {
69
+ out += ' ';
70
+ continue;
71
+ }
72
+ const next = cellStyle(cell);
73
+ if (next !== style) {
74
+ out += (style ? RESET : '') + (next ? `\x1b[${next}m` : '');
75
+ style = next;
76
+ }
77
+ out += cell.getChars() || ' ';
78
+ }
79
+ rows.push(style ? out + RESET : out);
80
+ }
81
+ return rows;
82
+ }
83
+ /**
84
+ * The screen as one string that, written to a cleared terminal at least as
85
+ * large, shows the same picture with the cursor in the same place.
86
+ */
87
+ export function screenSnapshot(term) {
88
+ const buffer = term.buffer.active;
89
+ const rows = renderRows(term);
90
+ let out = '\x1b[0m\x1b[2J';
91
+ rows.forEach((row, y) => {
92
+ out += `\x1b[${y + 1};1H${row}`;
93
+ });
94
+ return `${out}\x1b[${buffer.cursorY + 1};${buffer.cursorX + 1}H`;
95
+ }
96
+ //# sourceMappingURL=screen-render.js.map