@yuridulin/pointer 0.0.0-stage → 0.1.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 (71) hide show
  1. package/README.md +54 -2
  2. package/package.json +51 -4
  3. package/server/acp/catalog.ts +111 -0
  4. package/server/acp/connection.ts +233 -0
  5. package/server/acp/fake-agent.ts +408 -0
  6. package/server/acp/protocol.ts +167 -0
  7. package/server/acp/run.ts +139 -0
  8. package/server/acp/session.ts +1249 -0
  9. package/server/attachments.ts +115 -0
  10. package/server/changes.ts +239 -0
  11. package/server/cli.ts +362 -0
  12. package/server/compact.ts +361 -0
  13. package/server/config.ts +176 -0
  14. package/server/db.ts +462 -0
  15. package/server/dev.ts +8 -0
  16. package/server/diff.ts +190 -0
  17. package/server/engines.ts +63 -0
  18. package/server/files.ts +254 -0
  19. package/server/git.ts +319 -0
  20. package/server/history.ts +425 -0
  21. package/server/http/attachments.ts +63 -0
  22. package/server/http/browse.ts +108 -0
  23. package/server/http/chats.ts +627 -0
  24. package/server/http/closing.ts +14 -0
  25. package/server/http/files.ts +115 -0
  26. package/server/http/git.ts +84 -0
  27. package/server/http/preview.ts +21 -0
  28. package/server/http/providers.ts +200 -0
  29. package/server/http/reply.ts +38 -0
  30. package/server/hub.ts +521 -0
  31. package/server/install.ts +169 -0
  32. package/server/jobs.ts +664 -0
  33. package/server/linkPreview.ts +172 -0
  34. package/server/loop.ts +328 -0
  35. package/server/main.ts +353 -0
  36. package/server/probe.ts +34 -0
  37. package/server/process.ts +333 -0
  38. package/server/prompt.ts +196 -0
  39. package/server/providers/fake.ts +100 -0
  40. package/server/providers/opencode.ts +468 -0
  41. package/server/providers/registry.ts +149 -0
  42. package/server/providers/store.ts +210 -0
  43. package/server/segments.ts +248 -0
  44. package/server/sse.ts +93 -0
  45. package/server/tools/bash.ts +222 -0
  46. package/server/tools/edit.ts +254 -0
  47. package/server/tools/glob.ts +63 -0
  48. package/server/tools/grep.ts +122 -0
  49. package/server/tools/ignore.ts +54 -0
  50. package/server/tools/index.ts +129 -0
  51. package/server/tools/jail.ts +85 -0
  52. package/server/tools/jobs.ts +293 -0
  53. package/server/tools/read.ts +248 -0
  54. package/server/tools/write.ts +70 -0
  55. package/server/tray.ps1 +191 -0
  56. package/server/turn.ts +663 -0
  57. package/server/watch.ts +94 -0
  58. package/shared/acpConfig.ts +113 -0
  59. package/shared/links.ts +122 -0
  60. package/shared/types.ts +392 -0
  61. package/web/dist/app.css +1 -0
  62. package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
  63. package/web/dist/index.html +28 -0
  64. package/web/dist/main.css +1 -0
  65. package/web/dist/main.js +1366 -0
  66. package/web/dist/tokens.css +1 -0
  67. package/web/dist/workers/css.worker.js +97 -0
  68. package/web/dist/workers/editor.worker.js +30 -0
  69. package/web/dist/workers/html.worker.js +300 -0
  70. package/web/dist/workers/json.worker.js +62 -0
  71. package/web/dist/workers/ts.worker.js +67733 -0
package/server/jobs.ts ADDED
@@ -0,0 +1,664 @@
1
+ import { appendFileSync, mkdirSync, readdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+ import type { JobInfo, JobOutput, JobStatus } from '../shared/types'
4
+ import { pointerHome } from './config'
5
+ import { killTree, PROCESS_MARKER_ENV } from './process'
6
+
7
+ export const DEFAULT_JOB_TIMEOUT = 30
8
+ export const MAX_JOB_TIMEOUT = 300
9
+ export const MAX_OUTPUT = 30_000
10
+ export const RING_LIMIT = 200 * 1024
11
+ /** Disk cap for a spilled log. The in-memory ring stays at RING_LIMIT; the file keeps the start. */
12
+ export const SPILL_LIMIT = 32 * 1024 * 1024
13
+ const SPILL_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000
14
+ export const MAX_RUNNING_JOBS = 5
15
+ const FINISHED_TTL_MS = 60 * 60 * 1000
16
+ const KEEP_FINISHED = 20
17
+ const PREVIEW_LIMIT = 4_000
18
+
19
+ export type JobShell = { path: string }
20
+
21
+ export type JobCause = 'exit' | 'human' | 'model' | 'shutdown'
22
+
23
+ /** Wakes a running `wait` when a message enters the run queue. */
24
+ export type QueueWake = {
25
+ notify(): void
26
+ clear(): void
27
+ wait(signal?: AbortSignal): Promise<boolean>
28
+ }
29
+
30
+ export function createQueueWake(): QueueWake {
31
+ let pending = false
32
+ const listeners = new Set<() => void>()
33
+ return {
34
+ notify() {
35
+ pending = true
36
+ for (const listener of [...listeners]) listener()
37
+ listeners.clear()
38
+ },
39
+ clear() {
40
+ pending = false
41
+ },
42
+ wait(signal) {
43
+ if (pending) {
44
+ pending = false
45
+ return Promise.resolve(true)
46
+ }
47
+ if (signal?.aborted) return Promise.resolve(false)
48
+ return new Promise<boolean>((resolve) => {
49
+ const done = (value: boolean): void => {
50
+ listeners.delete(onWake)
51
+ signal?.removeEventListener('abort', onAbort)
52
+ resolve(value)
53
+ }
54
+ const onWake = (): void => done(true)
55
+ const onAbort = (): void => done(false)
56
+ listeners.add(onWake)
57
+ signal?.addEventListener('abort', onAbort, { once: true })
58
+ })
59
+ },
60
+ }
61
+ }
62
+
63
+ export type Job = {
64
+ id: string
65
+ chatId: string
66
+ command: string
67
+ cwd: string
68
+ status: JobStatus
69
+ exitCode: number | null
70
+ startedAt: number
71
+ finishedAt: number | null
72
+ delivered: boolean
73
+ ring: OutputRing
74
+ proc: ReturnType<typeof Bun.spawn>
75
+ done: Promise<void>
76
+ killCause: JobCause | null
77
+ /** Set as soon as the process has exited, before the output pumps finish draining. */
78
+ exited: boolean
79
+ shellPath: string
80
+ marker: string
81
+ resolveDone: () => void
82
+ }
83
+
84
+ export type JobCompletion = JobInfo & { preview: string }
85
+
86
+ export type JobsOptions = {
87
+ onStarted?: (job: JobInfo) => void
88
+ onOutput?: (chatId: string, jobId: string, chunk: string, offset: number) => void
89
+ onFinished?: (job: JobInfo, cause: JobCause) => void
90
+ maxRunning?: number
91
+ now?: () => number
92
+ }
93
+
94
+ export type StartJobInput = {
95
+ chatId: string
96
+ command: string
97
+ cwd: string
98
+ shell: JobShell
99
+ }
100
+
101
+ export type StartJobResult = { ok: true; job: Job } | { ok: false; error: string }
102
+
103
+ export type JobRegistry = {
104
+ start(input: StartJobInput): StartJobResult
105
+ list(chatId: string): JobInfo[]
106
+ get(chatId: string, jobId: string): Job | undefined
107
+ output(chatId: string, jobId: string, offset?: number, limit?: number): JobOutput | null
108
+ preview(chatId: string, jobId: string, limit?: number): { text: string; total: number; logPath: string | null } | null
109
+ kill(chatId: string, jobId: string, by: 'model' | 'human'): Promise<'killed' | 'already' | 'missing' | 'running'>
110
+ killChat(chatId: string): Promise<void>
111
+ stopAll(): Promise<void>
112
+ runningCount(): number
113
+ pending(chatId: string): boolean
114
+ takePending(chatId: string): JobCompletion[]
115
+ markDelivered(chatId: string, jobId: string): void
116
+ }
117
+
118
+ type ChatJobs = {
119
+ nextId: number
120
+ jobs: Map<string, Job>
121
+ pending: string[]
122
+ }
123
+
124
+ export function createJobRegistry(options: JobsOptions = {}): JobRegistry {
125
+ const chats = new Map<string, ChatJobs>()
126
+ const now = options.now ?? (() => Date.now())
127
+ const maxRunning = options.maxRunning ?? MAX_RUNNING_JOBS
128
+
129
+ function ensure(chatId: string): ChatJobs {
130
+ const existing = chats.get(chatId)
131
+ if (existing) return existing
132
+ const created: ChatJobs = { nextId: 1, jobs: new Map(), pending: [] }
133
+ chats.set(chatId, created)
134
+ return created
135
+ }
136
+
137
+ function info(job: Job): JobInfo {
138
+ return jobInfo(job)
139
+ }
140
+
141
+ function prune(chat: ChatJobs): void {
142
+ const finished = [...chat.jobs.values()]
143
+ .filter((job) => job.status !== 'running' && job.finishedAt !== null)
144
+ .sort((a, b) => (b.finishedAt ?? 0) - (a.finishedAt ?? 0))
145
+ const cutoff = now() - FINISHED_TTL_MS
146
+ const keep = new Set(finished.slice(0, KEEP_FINISHED).map((job) => job.id))
147
+ for (const job of finished) {
148
+ if (keep.has(job.id)) continue
149
+ if ((job.finishedAt ?? 0) >= cutoff) continue
150
+ chat.jobs.delete(job.id)
151
+ const index = chat.pending.indexOf(job.id)
152
+ if (index >= 0) chat.pending.splice(index, 1)
153
+ }
154
+ }
155
+
156
+ function lookup(chatId: string, jobId: string): Job | undefined {
157
+ const chat = chats.get(chatId)
158
+ if (!chat) return undefined
159
+ prune(chat)
160
+ return chat.jobs.get(jobId)
161
+ }
162
+
163
+ function markDelivered(chatId: string, jobId: string): void {
164
+ const chat = chats.get(chatId)
165
+ if (!chat) return
166
+ const job = chat.jobs.get(jobId)
167
+ if (job) job.delivered = true
168
+ const index = chat.pending.indexOf(jobId)
169
+ if (index >= 0) chat.pending.splice(index, 1)
170
+ }
171
+
172
+ function finish(job: Job, exitCode: number | null, killed: boolean): void {
173
+ if (job.status !== 'running') return
174
+ const cause: JobCause = killed ? (job.killCause ?? 'shutdown') : 'exit'
175
+ job.exitCode = exitCode
176
+ job.status = cause === 'exit' ? 'finished' : 'killed'
177
+ job.finishedAt = now()
178
+ // A model kill stays pending until the tool row is actually kept. Abort discards that
179
+ // row, and the completion would otherwise vanish with delivered already set.
180
+ const chat = chats.get(job.chatId)
181
+ if (chat && cause !== 'shutdown' && !job.delivered && !chat.pending.includes(job.id)) {
182
+ chat.pending.push(job.id)
183
+ }
184
+ // Resolve before the callback: a `wait` that raced the exit must see the new status.
185
+ job.resolveDone()
186
+ if (chat) options.onFinished?.(info(job), cause)
187
+ }
188
+
189
+ return {
190
+ start(input) {
191
+ const chat = ensure(input.chatId)
192
+ prune(chat)
193
+ const running = [...chat.jobs.values()].filter((job) => job.status === 'running').length
194
+ if (running >= maxRunning) {
195
+ return {
196
+ ok: false,
197
+ error: `Too many background jobs: ${running} already running, the limit is ${maxRunning}. Wait for one to finish or kill it.`,
198
+ }
199
+ }
200
+
201
+ const marker = process.platform === 'win32' ? crypto.randomUUID() : ''
202
+ const command = `exec 2>&1\n${input.command}`
203
+ let proc: ReturnType<typeof Bun.spawn>
204
+ try {
205
+ proc = Bun.spawn([input.shell.path, '-c', command], {
206
+ cwd: input.cwd,
207
+ stdout: 'pipe',
208
+ stderr: 'pipe',
209
+ stdin: 'ignore',
210
+ ...(marker ? { env: { ...process.env, [PROCESS_MARKER_ENV]: marker } } : {}),
211
+ ...(process.platform === 'win32' ? {} : { detached: true }),
212
+ })
213
+ } catch (error) {
214
+ return { ok: false, error: error instanceof Error ? error.message : String(error) }
215
+ }
216
+
217
+ let resolveDone: () => void = () => {}
218
+ const done = new Promise<void>((resolve) => {
219
+ resolveDone = resolve
220
+ })
221
+ const id = `j${chat.nextId++}`
222
+ const job: Job = {
223
+ id,
224
+ chatId: input.chatId,
225
+ command: input.command,
226
+ cwd: input.cwd,
227
+ status: 'running',
228
+ exitCode: null,
229
+ startedAt: now(),
230
+ finishedAt: null,
231
+ delivered: false,
232
+ ring: new OutputRing(input.chatId),
233
+ proc,
234
+ done,
235
+ killCause: null,
236
+ exited: false,
237
+ shellPath: input.shell.path,
238
+ marker,
239
+ resolveDone,
240
+ }
241
+ chat.jobs.set(id, job)
242
+ options.onStarted?.(info(job))
243
+
244
+ const pumps = [
245
+ pumpStream(proc.stdout as ReadableStream<Uint8Array>, (chunk) => push(job, chunk, options)),
246
+ pumpStream(proc.stderr as ReadableStream<Uint8Array>, (chunk) => push(job, chunk, options)),
247
+ ]
248
+ void (async () => {
249
+ let exitCode: number | null = null
250
+ try {
251
+ exitCode = await proc.exited
252
+ } catch {}
253
+ // Snapshot the kill before the drain. A kill that arrives while the pipes flush
254
+ // must not turn a natural exit into status killed, and status is still running here.
255
+ job.exited = true
256
+ const killed = job.killCause !== null
257
+ await drainPumps(pumps, killed)
258
+ finish(job, exitCode, killed)
259
+ })()
260
+ return { ok: true, job }
261
+ },
262
+
263
+ list(chatId) {
264
+ const chat = chats.get(chatId)
265
+ if (!chat) return []
266
+ prune(chat)
267
+ return [...chat.jobs.values()].map(info)
268
+ },
269
+
270
+ get(chatId, jobId) {
271
+ const chat = chats.get(chatId)
272
+ if (!chat) return undefined
273
+ prune(chat)
274
+ return chat.jobs.get(jobId)
275
+ },
276
+
277
+ output(chatId, jobId, offset = 0, limit = MAX_OUTPUT) {
278
+ const job = lookup(chatId, jobId)
279
+ if (!job) return null
280
+ return outputOf(job, offset, limit)
281
+ },
282
+
283
+ preview(chatId, jobId, limit = PREVIEW_LIMIT) {
284
+ const job = lookup(chatId, jobId)
285
+ if (!job) return null
286
+ const start = Math.max(0, job.ring.total - limit)
287
+ return {
288
+ text: job.ring.sliceFrom(start),
289
+ total: job.ring.total,
290
+ logPath: job.ring.spill()?.path ?? null,
291
+ }
292
+ },
293
+
294
+ async kill(chatId, jobId, by) {
295
+ const job = lookup(chatId, jobId)
296
+ if (!job) return 'missing'
297
+ if (job.status !== 'running' || job.exited) return 'already'
298
+ if (job.killCause !== null) {
299
+ await settle(job, 2_000)
300
+ return job.status === 'running' ? 'running' : 'already'
301
+ }
302
+ job.killCause = by
303
+ const marker = process.platform === 'win32' ? job.marker : ''
304
+ await killTree(job.proc.pid, job.shellPath, marker)
305
+ // `killTree` waits for the killer, not for the process. Give the exit handler a
306
+ // moment so `job_kill` does not answer before the status changed.
307
+ await settle(job, 2_000)
308
+ return job.status === 'running' ? 'running' : 'killed'
309
+ },
310
+
311
+ async killChat(chatId) {
312
+ const chat = chats.get(chatId)
313
+ if (!chat) return
314
+ const running = [...chat.jobs.values()].filter((job) => job.status === 'running')
315
+ for (const job of running) {
316
+ if (job.killCause === null && !job.exited) job.killCause = 'shutdown'
317
+ }
318
+ await Promise.all(
319
+ running.map(async (job) => {
320
+ if (!job.exited) {
321
+ const marker = process.platform === 'win32' ? job.marker : ''
322
+ await killTree(job.proc.pid, job.shellPath, marker)
323
+ }
324
+ await settle(job, 2_000)
325
+ }),
326
+ )
327
+ chats.delete(chatId)
328
+ },
329
+
330
+ runningCount() {
331
+ let count = 0
332
+ for (const chat of chats.values()) {
333
+ for (const job of chat.jobs.values()) {
334
+ if (job.status === 'running') count += 1
335
+ }
336
+ }
337
+ return count
338
+ },
339
+
340
+ async stopAll() {
341
+ const all: Job[] = []
342
+ for (const chat of chats.values()) {
343
+ for (const job of chat.jobs.values()) {
344
+ if (job.status === 'running') all.push(job)
345
+ }
346
+ }
347
+ for (const job of all) {
348
+ if (job.killCause === null && !job.exited) job.killCause = 'shutdown'
349
+ }
350
+ await Promise.all(
351
+ all.map(async (job) => {
352
+ if (!job.exited) {
353
+ const marker = process.platform === 'win32' ? job.marker : ''
354
+ await killTree(job.proc.pid, job.shellPath, marker)
355
+ }
356
+ await settle(job, 2_000)
357
+ }),
358
+ )
359
+ chats.clear()
360
+ },
361
+
362
+ pending(chatId) {
363
+ const chat = chats.get(chatId)
364
+ if (!chat) return false
365
+ return chat.pending.some((id) => {
366
+ const job = chat.jobs.get(id)
367
+ return job !== undefined && job.status !== 'running' && !job.delivered
368
+ })
369
+ },
370
+
371
+ takePending(chatId) {
372
+ const chat = chats.get(chatId)
373
+ if (!chat) return []
374
+ const results: JobCompletion[] = []
375
+ for (const id of chat.pending) {
376
+ const job = chat.jobs.get(id)
377
+ if (!job || job.status === 'running' || job.delivered) continue
378
+ job.delivered = true
379
+ const start = Math.max(0, job.ring.total - PREVIEW_LIMIT)
380
+ results.push({ ...info(job), preview: job.ring.sliceFrom(start) })
381
+ }
382
+ chat.pending = []
383
+ return results
384
+ },
385
+
386
+ markDelivered,
387
+ }
388
+ }
389
+
390
+ async function settle(job: Job, ms: number): Promise<void> {
391
+ let timer: ReturnType<typeof setTimeout> | undefined
392
+ await Promise.race([
393
+ job.done,
394
+ new Promise<void>((resolve) => {
395
+ timer = setTimeout(resolve, ms)
396
+ }),
397
+ ])
398
+ if (timer !== undefined) clearTimeout(timer)
399
+ }
400
+
401
+ function outputOf(job: Job, requestedOffset: number, limit: number): JobOutput {
402
+ const total = job.ring.total
403
+ const ringStart = total - job.ring.size
404
+ const requested = Math.max(0, Math.min(Math.floor(Number.isFinite(requestedOffset) ? requestedOffset : 0), total))
405
+ const start = Math.max(requested, ringStart)
406
+ const available = job.ring.sliceFrom(start)
407
+ const overLimit = available.length > limit
408
+ const text = overLimit ? cutLeadingSurrogate(available.slice(available.length - limit)) : available
409
+ // `sliceFrom` may drop one code unit when `start` lands on a low surrogate. The window
410
+ // still ends at `total`, or the next chunk looks like a gap and the client resets it.
411
+ const offset = total - text.length
412
+ const spill = job.ring.spill()
413
+ return {
414
+ ...jobInfo(job),
415
+ requestedOffset: requested,
416
+ offset,
417
+ nextOffset: total,
418
+ text,
419
+ clipped: start > requested,
420
+ truncated: overLimit,
421
+ logPath: spill?.path ?? null,
422
+ }
423
+ }
424
+
425
+ /** Tail shown to the model, with an explicit note when it is shorter than the output. */
426
+ export function formatJobPreview(preview: string, total: number, logPath: string | null): string {
427
+ const body = preview.replace(/\n+$/, '')
428
+ const parts: string[] = []
429
+ if (body) parts.push(body)
430
+ if (total > preview.length) {
431
+ parts.push(
432
+ logPath
433
+ ? `Output truncated: showing the last ${preview.length} of ${total} chars. Full output: ${logPath}`
434
+ : `Output truncated: showing the last ${preview.length} of ${total} chars. Read the rest with job_output, up to ${MAX_OUTPUT} chars.`,
435
+ )
436
+ } else if (logPath) {
437
+ parts.push(`Full output: ${logPath}`)
438
+ }
439
+ return parts.join('\n')
440
+ }
441
+
442
+ function jobInfo(job: Job): JobInfo {
443
+ const spill = job.ring.spill()
444
+ return {
445
+ id: job.id,
446
+ chatId: job.chatId,
447
+ command: job.command,
448
+ status: job.status,
449
+ exitCode: job.exitCode,
450
+ outputSize: job.ring.total,
451
+ startedAt: job.startedAt,
452
+ finishedAt: job.finishedAt,
453
+ logPath: spill?.path ?? null,
454
+ }
455
+ }
456
+
457
+
458
+ function push(job: Job, chunk: string, options: JobsOptions): void {
459
+ if (!chunk) return
460
+ job.ring.push(chunk)
461
+ options.onOutput?.(job.chatId, job.id, chunk, job.ring.total - chunk.length)
462
+ }
463
+
464
+ export type Pump = { done: Promise<void>; stop(): void }
465
+
466
+ /** One stream into a callback; used by foreground bash and by background jobs. */
467
+ export function pumpStream(
468
+ stream: ReadableStream<Uint8Array>,
469
+ onChunk: (chunk: string) => void,
470
+ ): Pump {
471
+ const decoder = new TextDecoder('utf-8')
472
+ const reader = stream.getReader()
473
+ const done = (async () => {
474
+ try {
475
+ for (;;) {
476
+ const { done, value } = await reader.read()
477
+ if (done) break
478
+ const text = decoder.decode(value, { stream: true })
479
+ if (text) onChunk(text)
480
+ }
481
+ const rest = decoder.decode()
482
+ if (rest) onChunk(rest)
483
+ } finally {
484
+ reader.releaseLock()
485
+ }
486
+ })()
487
+ return {
488
+ done: done.catch(() => {}),
489
+ stop: () => {
490
+ void reader.cancel().catch(() => {})
491
+ },
492
+ }
493
+ }
494
+
495
+ /**
496
+ * A killed shell takes its pipes with it, but a surviving background child may hold them open
497
+ * forever. Give it a short grace period to flush, then drop the remaining streams.
498
+ */
499
+ export async function drainPumps(pumps: Pump[], killed: boolean): Promise<void> {
500
+ let timer: ReturnType<typeof setTimeout> | undefined
501
+ const flushed = await Promise.race([
502
+ Promise.all(pumps.map((item) => item.done)).then(() => true),
503
+ new Promise<boolean>((resolve) => {
504
+ timer = setTimeout(() => resolve(false), killed ? 250 : 500)
505
+ }),
506
+ ])
507
+ clearTimeout(timer)
508
+ if (flushed) return
509
+ for (const item of pumps) item.stop()
510
+ await Promise.all(pumps.map((item) => item.done))
511
+ }
512
+
513
+ export class OutputRing {
514
+ total = 0
515
+ private text = ''
516
+ private spillPath: string | null = null
517
+ private spillChars = 0
518
+ private spillStarted = false
519
+
520
+ constructor(private readonly chatId = '') {}
521
+
522
+ get size(): number {
523
+ return this.text.length
524
+ }
525
+
526
+ push(chunk: string): void {
527
+ this.total += chunk.length
528
+ const startSpill = !this.spillStarted && this.total > MAX_OUTPUT
529
+ this.text += chunk
530
+ // The ring still holds the whole output here: MAX_OUTPUT is below RING_LIMIT,
531
+ // so the first spill writes the start that a later trim would drop.
532
+ if (startSpill) {
533
+ this.spillStarted = true
534
+ const opened = openSpill(this.chatId, this.text)
535
+ if (opened) {
536
+ this.spillPath = opened.path
537
+ this.spillChars = opened.chars
538
+ }
539
+ } else if (this.spillPath) {
540
+ this.appendSpill(chunk)
541
+ }
542
+ if (this.text.length > RING_LIMIT) {
543
+ this.text = cutLeadingSurrogate(this.text.slice(this.text.length - RING_LIMIT))
544
+ }
545
+ }
546
+
547
+ /** Text from an absolute output offset; offsets before the ring start are clipped to it. */
548
+ sliceFrom(offset: number): string {
549
+ const start = this.total - this.text.length
550
+ if (offset <= start) return this.text
551
+ return cutLeadingSurrogate(this.text.slice(offset - start))
552
+ }
553
+
554
+ tail(): { text: string; truncated: boolean } {
555
+ const truncated = this.total > MAX_OUTPUT
556
+ if (!truncated) return { text: this.text, truncated }
557
+ return { text: cutLeadingSurrogate(this.text.slice(-MAX_OUTPUT)), truncated }
558
+ }
559
+
560
+ spill(): { path: string; chars: number } | null {
561
+ if (!this.spillPath) return null
562
+ return { path: this.spillPath, chars: this.spillChars }
563
+ }
564
+
565
+ discardSpill(): void {
566
+ const path = this.spillPath
567
+ this.spillPath = null
568
+ if (!path) return
569
+ try {
570
+ unlinkSync(path)
571
+ } catch {
572
+ // Already gone. The abort result does not mention the path.
573
+ }
574
+ }
575
+
576
+ private appendSpill(chunk: string): void {
577
+ if (!this.spillPath || this.spillChars >= SPILL_LIMIT) return
578
+ const piece = bounded(chunk, SPILL_LIMIT - this.spillChars)
579
+ if (!piece) return
580
+ try {
581
+ appendFileSync(this.spillPath, piece, 'utf8')
582
+ this.spillChars += piece.length
583
+ } catch {
584
+ this.spillPath = null
585
+ }
586
+ }
587
+ }
588
+
589
+ /** Directory of spilled command logs for one chat, so a grep cannot read another workspace. */
590
+ export function bashOutputDir(chatId?: string): string {
591
+ const root = join(pointerHome(), 'bash')
592
+ return chatId ? join(root, chatId) : root
593
+ }
594
+
595
+ /** Deletes spilled logs older than a week. Missing directory is not an error. */
596
+ export function pruneBashLogs(dir: string, now = Date.now()): void {
597
+ let names: string[]
598
+ try {
599
+ names = readdirSync(dir)
600
+ } catch {
601
+ return
602
+ }
603
+ for (const name of names) {
604
+ const file = join(dir, name)
605
+ let info
606
+ try {
607
+ info = statSync(file)
608
+ } catch {
609
+ continue
610
+ }
611
+ if (info.isDirectory()) {
612
+ pruneBashLogs(file, now)
613
+ continue
614
+ }
615
+ if (!name.endsWith('.log')) continue
616
+ try {
617
+ if (now - info.mtimeMs > SPILL_MAX_AGE_MS) unlinkSync(file)
618
+ } catch {
619
+ // A log that is still open, or already gone, is left for the next pass.
620
+ }
621
+ }
622
+ }
623
+
624
+ function openSpill(chatId: string, text: string): { path: string; chars: number } | null {
625
+ try {
626
+ const dir = bashOutputDir(chatId)
627
+ mkdirSync(dir, { recursive: true })
628
+ pruneBashLogs(dir)
629
+ const file = join(dir, `${Date.now()}-${crypto.randomUUID()}.log`)
630
+ const body = bounded(text, SPILL_LIMIT)
631
+ writeFileSync(file, body, 'utf8')
632
+ return { path: file.replaceAll('\\', '/'), chars: body.length }
633
+ } catch {
634
+ return null
635
+ }
636
+ }
637
+
638
+ function bounded(text: string, limit: number): string {
639
+ if (text.length <= limit) return text
640
+ let body = text.slice(0, limit)
641
+ const last = body.charCodeAt(body.length - 1)
642
+ if (last >= 0xd800 && last <= 0xdbff) body = body.slice(0, -1)
643
+ return body
644
+ }
645
+
646
+ export function pruneJobInfos(jobs: JobInfo[], now = Date.now()): JobInfo[] {
647
+ const finished = jobs
648
+ .filter((job) => job.status !== 'running' && job.finishedAt !== null)
649
+ .sort((a, b) => (b.finishedAt ?? 0) - (a.finishedAt ?? 0))
650
+ const cutoff = now - FINISHED_TTL_MS
651
+ const keep = new Set(finished.slice(0, KEEP_FINISHED).map((job) => job.id))
652
+ for (const job of finished) {
653
+ if (keep.has(job.id)) continue
654
+ if ((job.finishedAt ?? 0) >= cutoff) keep.add(job.id)
655
+ }
656
+ return jobs.filter((job) => job.status === 'running' || keep.has(job.id))
657
+ }
658
+
659
+ export function cutLeadingSurrogate(text: string): string {
660
+ if (text.length > 0 && text.charCodeAt(0) >= 0xdc00 && text.charCodeAt(0) <= 0xdfff) {
661
+ return text.slice(1)
662
+ }
663
+ return text
664
+ }