adhdev 0.8.12 → 0.8.13

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.
@@ -10,7 +10,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
10
10
  import { randomUUID } from "crypto";
11
11
  import * as fs4 from "fs";
12
12
  import * as os3 from "os";
13
- import * as path3 from "path";
13
+ import * as path2 from "path";
14
14
  import {
15
15
  SessionHostClient,
16
16
  formatRuntimeOwner,
@@ -33,47 +33,20 @@ import {
33
33
  // src/runtime.ts
34
34
  import * as fs from "fs";
35
35
  import * as os from "os";
36
- import * as path from "path";
37
36
  import * as pty from "node-pty";
37
+ import { sanitizeSpawnEnv, ensureNodePtySpawnHelperPermissions } from "@adhdev/session-host-core";
38
38
  var terminalMirrorFactory;
39
39
  var terminalMirrorWarning = null;
40
- if (os.platform() !== "win32") {
41
- try {
42
- const fs5 = __require("fs");
43
- const ptyDir = path.resolve(path.dirname(__require.resolve("node-pty")), "..");
44
- const platformArch = `${os.platform()}-${os.arch()}`;
45
- const helper = path.join(ptyDir, "prebuilds", platformArch, "spawn-helper");
46
- if (fs5.existsSync(helper)) {
47
- const stat = fs5.statSync(helper);
48
- if (!(stat.mode & 73)) {
49
- fs5.chmodSync(helper, stat.mode | 493);
50
- }
51
- }
52
- } catch {
53
- }
54
- }
55
- function buildRuntimeEnv(baseEnv, launchEnv) {
56
- const env = {};
57
- const source = { ...baseEnv, ...launchEnv || {} };
58
- for (const [key, value] of Object.entries(source)) {
59
- if (typeof value !== "string") continue;
60
- env[key] = value;
61
- }
62
- for (const key of Object.keys(env)) {
63
- if (key === "INIT_CWD" || key === "npm_command" || key === "npm_execpath" || key === "npm_node_execpath" || key.startsWith("npm_") || key.startsWith("npm_config_") || key.startsWith("npm_package_") || key.startsWith("npm_lifecycle_") || key.startsWith("PNPM_") || key.startsWith("YARN_") || key.startsWith("BUN_")) {
64
- delete env[key];
65
- }
66
- }
67
- if (!env.NO_COLOR) {
68
- if (!env.TERM || env.TERM === "xterm-color") env.TERM = "xterm-256color";
69
- if (!env.COLORTERM) env.COLORTERM = "truecolor";
70
- if (process.platform === "win32") {
71
- if (!env.FORCE_COLOR) env.FORCE_COLOR = "1";
72
- if (!env.CLICOLOR) env.CLICOLOR = "1";
73
- }
74
- }
75
- return env;
40
+ var terminalMirrorBackendLogged = false;
41
+ ensureNodePtySpawnHelperPermissions((msg) => console.log(`[session-host] ${msg}`));
42
+ function logTerminalMirrorBackend(message, level = "info") {
43
+ if (terminalMirrorBackendLogged) return;
44
+ terminalMirrorBackendLogged = true;
45
+ const prefix = "[session-host]";
46
+ if (level === "warn") console.warn(`${prefix} ${message}`);
47
+ else console.log(`${prefix} ${message}`);
76
48
  }
49
+ var buildRuntimeEnv = sanitizeSpawnEnv;
77
50
  function computeTerminalQueryTail(buffer) {
78
51
  const prefixes = ["\x1B[6n", "\x1B[?6n"];
79
52
  const maxLength = prefixes.reduce((n, value) => Math.max(n, value.length), 0) - 1;
@@ -178,15 +151,18 @@ function getTerminalMirrorFactory() {
178
151
  throw new Error("@adhdev/ghostty-vt-node does not export createTerminal()");
179
152
  }
180
153
  terminalMirrorFactory = (options) => binding.createTerminal(options);
154
+ logTerminalMirrorBackend("terminal mirror backend=ghostty-vt");
181
155
  return terminalMirrorFactory;
182
156
  } catch (ghosttyError) {
183
157
  try {
184
158
  terminalMirrorFactory = createXtermMirror;
185
159
  terminalMirrorWarning = `Ghostty VT unavailable; falling back to xterm mirror (${ghosttyError?.message || String(ghosttyError)})`;
160
+ logTerminalMirrorBackend(terminalMirrorWarning, "warn");
186
161
  return terminalMirrorFactory;
187
162
  } catch (xtermError) {
188
163
  terminalMirrorFactory = null;
189
164
  terminalMirrorWarning = `No terminal mirror backend available (ghostty: ${ghosttyError?.message || String(ghosttyError)}; xterm: ${xtermError?.message || String(xtermError)})`;
165
+ logTerminalMirrorBackend(terminalMirrorWarning, "warn");
190
166
  throw new Error(terminalMirrorWarning);
191
167
  }
192
168
  }
@@ -284,14 +260,14 @@ var PtySessionRuntime = class {
284
260
  // src/storage.ts
285
261
  import * as fs2 from "fs";
286
262
  import * as os2 from "os";
287
- import * as path2 from "path";
263
+ import * as path from "path";
288
264
  var SessionHostStorage = class {
289
265
  rootDir;
290
266
  runtimesDir;
291
267
  constructor(options = {}) {
292
268
  const appName = options.appName || "adhdev";
293
- this.rootDir = path2.join(os2.homedir(), ".adhdev", "session-host", appName);
294
- this.runtimesDir = path2.join(this.rootDir, "runtimes");
269
+ this.rootDir = path.join(os2.homedir(), ".adhdev", "session-host", appName);
270
+ this.runtimesDir = path.join(this.rootDir, "runtimes");
295
271
  }
296
272
  loadAll() {
297
273
  if (!fs2.existsSync(this.runtimesDir)) return [];
@@ -299,7 +275,7 @@ var SessionHostStorage = class {
299
275
  const states = [];
300
276
  for (const entry of entries) {
301
277
  if (!entry.isFile() || !entry.name.endsWith(".json")) continue;
302
- const fullPath = path2.join(this.runtimesDir, entry.name);
278
+ const fullPath = path.join(this.runtimesDir, entry.name);
303
279
  try {
304
280
  const parsed = JSON.parse(fs2.readFileSync(fullPath, "utf8"));
305
281
  if (parsed?.record?.sessionId) {
@@ -312,7 +288,7 @@ var SessionHostStorage = class {
312
288
  }
313
289
  save(record, snapshot) {
314
290
  fs2.mkdirSync(this.runtimesDir, { recursive: true });
315
- const filePath = path2.join(this.runtimesDir, `${record.sessionId}.json`);
291
+ const filePath = path.join(this.runtimesDir, `${record.sessionId}.json`);
316
292
  const payload = {
317
293
  record,
318
294
  snapshot,
@@ -321,7 +297,7 @@ var SessionHostStorage = class {
321
297
  fs2.writeFileSync(filePath, JSON.stringify(payload, null, 2), "utf8");
322
298
  }
323
299
  remove(sessionId) {
324
- const filePath = path2.join(this.runtimesDir, `${sessionId}.json`);
300
+ const filePath = path.join(this.runtimesDir, `${sessionId}.json`);
325
301
  try {
326
302
  fs2.unlinkSync(filePath);
327
303
  } catch {
@@ -360,8 +336,8 @@ var SessionHostServer = class extends EventEmitter {
360
336
  void this.handleIncomingRequest(socket, envelope);
361
337
  }));
362
338
  });
363
- await new Promise((resolve2, reject) => {
364
- this.ipcServer?.once("listening", () => resolve2());
339
+ await new Promise((resolve, reject) => {
340
+ this.ipcServer?.once("listening", () => resolve());
365
341
  this.ipcServer?.once("error", reject);
366
342
  this.ipcServer?.listen(this.endpoint.path);
367
343
  });
@@ -394,7 +370,7 @@ var SessionHostServer = class extends EventEmitter {
394
370
  if (this.ipcServer) {
395
371
  const server = this.ipcServer;
396
372
  this.ipcServer = null;
397
- await new Promise((resolve2) => server.close(() => resolve2()));
373
+ await new Promise((resolve) => server.close(() => resolve()));
398
374
  }
399
375
  if (this.endpoint.kind === "unix") {
400
376
  try {
@@ -709,9 +685,9 @@ var SessionHostServer = class extends EventEmitter {
709
685
  // src/index.ts
710
686
  var SESSION_HOST_APP_NAME = process.env.ADHDEV_SESSION_HOST_NAME || "adhdev";
711
687
  function getSessionHostPidFile(appName) {
712
- const dir = path3.join(os3.homedir(), ".adhdev");
688
+ const dir = path2.join(os3.homedir(), ".adhdev");
713
689
  if (!fs4.existsSync(dir)) fs4.mkdirSync(dir, { recursive: true });
714
- return path3.join(dir, `${appName}-session-host.pid`);
690
+ return path2.join(dir, `${appName}-session-host.pid`);
715
691
  }
716
692
  function writeSessionHostPid(appName) {
717
693
  fs4.writeFileSync(getSessionHostPidFile(appName), String(process.pid), "utf8");
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/server.ts","../src/runtime.ts","../src/storage.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { randomUUID } from 'crypto';\nimport * as fs from 'fs';\nimport * as os from 'os';\nimport * as path from 'path';\nimport {\n SessionHostClient,\n formatRuntimeOwner,\n getDefaultSessionHostEndpoint,\n resolveRuntimeRecord,\n type SessionHostEvent,\n type SessionHostRecord,\n} from '@adhdev/session-host-core';\nimport { SessionHostServer } from './server.js';\n\nexport { SessionHostServer } from './server.js';\nexport type { SessionHostServerOptions } from './server.js';\n\nconst SESSION_HOST_APP_NAME = process.env.ADHDEV_SESSION_HOST_NAME || 'adhdev';\n\nfunction getSessionHostPidFile(appName: string): string {\n const dir = path.join(os.homedir(), '.adhdev');\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });\n return path.join(dir, `${appName}-session-host.pid`);\n}\n\nfunction writeSessionHostPid(appName: string): void {\n fs.writeFileSync(getSessionHostPidFile(appName), String(process.pid), 'utf8');\n}\n\nfunction removeSessionHostPid(appName: string): void {\n try {\n fs.unlinkSync(getSessionHostPidFile(appName));\n } catch {\n // noop\n }\n}\n\nfunction parseArgs(argv: string[]) {\n const [command, ...rest] = argv;\n const readOnly = rest.includes('--read-only');\n const takeover = rest.includes('--takeover');\n const showAll = rest.includes('--all');\n const positional = rest.filter((arg) => arg !== '--read-only' && arg !== '--takeover' && arg !== '--all');\n return {\n command: command || 'serve',\n positional,\n readOnly,\n takeover,\n showAll,\n };\n}\n\nasync function runServer(): Promise<void> {\n const server = new SessionHostServer({ appName: SESSION_HOST_APP_NAME });\n writeSessionHostPid(SESSION_HOST_APP_NAME);\n await server.start();\n\n process.on('SIGINT', async () => {\n await server.stop();\n removeSessionHostPid(SESSION_HOST_APP_NAME);\n process.exit(0);\n });\n\n process.on('SIGTERM', async () => {\n await server.stop();\n removeSessionHostPid(SESSION_HOST_APP_NAME);\n process.exit(0);\n });\n\n // Fallback: flush persistence on any exit (covers Windows where SIGTERM is unsupported)\n process.on('exit', () => {\n server.flushAllPersistence();\n removeSessionHostPid(SESSION_HOST_APP_NAME);\n });\n\n // Keep the host alive; IPC transport wiring comes next.\n await new Promise<void>(() => {});\n}\n\nasync function listRuntimes(showAll = false): Promise<void> {\n const client = new SessionHostClient({ endpoint: getDefaultSessionHostEndpoint(SESSION_HOST_APP_NAME) });\n try {\n const response = await client.request<SessionHostRecord[]>({\n type: 'list_sessions',\n payload: {},\n });\n if (!response.success) {\n throw new Error(response.error || 'Failed to list runtimes');\n }\n const runtimes = (response.result || []).filter((runtime: SessionHostRecord) => showAll || runtime.lifecycle !== 'stopped');\n if (runtimes.length === 0) {\n console.log('No runtimes.');\n return;\n }\n console.log('runtimeKey\\tlifecycle\\towner\\tworkspace\\tid\\tdisplayName');\n for (const runtime of runtimes) {\n console.log([\n runtime.runtimeKey,\n runtime.lifecycle,\n formatRuntimeOwner(runtime),\n runtime.workspaceLabel,\n runtime.sessionId,\n runtime.displayName,\n ].join('\\t'));\n }\n } finally {\n await client.close().catch(() => {});\n }\n}\n\nasync function attachRuntime(target: string, readOnly = false, takeover = false): Promise<void> {\n const client = new SessionHostClient({ endpoint: getDefaultSessionHostEndpoint(SESSION_HOST_APP_NAME) });\n const clientId = `local-terminal-${process.pid}-${randomUUID().slice(0, 8)}`;\n let lastSeq = 0;\n let restoredRawMode = false;\n let runtimeId = '';\n let localReadOnly = readOnly;\n\n const cleanup = async () => {\n process.stdout.off('resize', handleResize);\n process.stdin.off('data', handleInput);\n process.stdin.pause();\n if (process.stdin.isTTY && restoredRawMode) {\n process.stdin.setRawMode(false);\n }\n await client.request({\n type: 'release_write',\n payload: {\n sessionId: runtimeId,\n clientId,\n },\n }).catch(() => ({ success: false }));\n await client.request({\n type: 'detach_session',\n payload: {\n sessionId: runtimeId,\n clientId,\n },\n }).catch(() => ({ success: false }));\n await client.close().catch(() => {});\n };\n\n const handleResize = () => {\n void client.request({\n type: 'resize_session',\n payload: {\n sessionId: runtimeId,\n cols: process.stdout.columns || 80,\n rows: process.stdout.rows || 24,\n },\n }).catch(() => ({ success: false }));\n };\n\n const sendInputWithTakeover = async (data: string) => {\n let response = await client.request({\n type: 'send_input',\n payload: {\n sessionId: runtimeId,\n clientId,\n data,\n },\n });\n if (!response.success && response.error?.startsWith('Write owned by ')) {\n const ownerResponse = await client.request<SessionHostRecord>({\n type: 'acquire_write',\n payload: {\n sessionId: runtimeId,\n clientId,\n ownerType: 'user',\n force: true,\n },\n });\n if (ownerResponse.success && ownerResponse.result) {\n response = await client.request({\n type: 'send_input',\n payload: {\n sessionId: runtimeId,\n clientId,\n data,\n },\n });\n if (response.success) {\n process.stderr.write(`Took control of ${ownerResponse.result.runtimeKey}.\\n`);\n }\n }\n }\n return response;\n };\n\n const handleInput = (chunk: Buffer) => {\n if (!localReadOnly && chunk.length === 1 && chunk[0] === 0x1d) {\n void cleanup().finally(() => process.exit(0));\n return;\n }\n if (localReadOnly) return;\n void sendInputWithTakeover(chunk.toString('utf8')).catch(() => ({ success: false }));\n };\n\n try {\n if (readOnly && takeover) {\n throw new Error('Use either --read-only or --takeover, not both');\n }\n\n const listResponse = await client.request<SessionHostRecord[]>({\n type: 'list_sessions',\n payload: {},\n });\n if (!listResponse.success || !listResponse.result) {\n throw new Error(listResponse.error || 'Failed to list runtimes');\n }\n let runtimeRecord = resolveRuntimeRecord(listResponse.result, target);\n runtimeId = runtimeRecord.sessionId;\n\n if (runtimeRecord.lifecycle === 'interrupted' && !readOnly) {\n const resumeResponse = await client.request<SessionHostRecord>({\n type: 'resume_session',\n payload: {\n sessionId: runtimeId,\n },\n });\n if (resumeResponse.success && resumeResponse.result) {\n runtimeRecord = resumeResponse.result;\n } else {\n process.stderr.write(\n `Runtime ${runtimeRecord.runtimeKey} could not be resumed automatically: ${resumeResponse.error || 'unknown error'}\\n`,\n );\n }\n }\n\n let effectiveReadOnly = readOnly;\n if (!effectiveReadOnly && runtimeRecord.writeOwner && runtimeRecord.writeOwner.clientId !== clientId && !takeover) {\n process.stderr.write(\n `Runtime ${runtimeRecord.runtimeKey} is currently owned by ${runtimeRecord.writeOwner.clientId}; first input will take control here.\\n`,\n );\n }\n localReadOnly = effectiveReadOnly;\n\n const attachResponse = await client.request<SessionHostRecord>({\n type: 'attach_session',\n payload: {\n sessionId: runtimeId,\n clientId,\n clientType: 'local-terminal',\n readOnly: effectiveReadOnly,\n },\n });\n if (!attachResponse.success) {\n throw new Error(attachResponse.error || `Failed to attach runtime ${runtimeId}`);\n }\n const attachedRecord = attachResponse.result || null;\n\n if (!effectiveReadOnly && takeover) {\n const ownerResponse = await client.request<SessionHostRecord>({\n type: 'acquire_write',\n payload: {\n sessionId: runtimeId,\n clientId,\n ownerType: 'user',\n force: takeover,\n },\n });\n if (!ownerResponse.success) {\n throw new Error(ownerResponse.error || `Failed to acquire write owner for runtime ${runtimeId}`);\n }\n }\n\n const snapshotResponse = await client.request<{ seq: number; text: string; truncated: boolean; cols?: number; rows?: number }>({\n type: 'get_snapshot',\n payload: { sessionId: runtimeId },\n });\n if (!snapshotResponse.success) {\n throw new Error(snapshotResponse.error || `Failed to read runtime snapshot ${runtimeId}`);\n }\n lastSeq = snapshotResponse.result?.seq || 0;\n if (snapshotResponse.result?.text) {\n process.stdout.write(snapshotResponse.result.text);\n }\n if (attachedRecord?.lifecycle === 'stopped' || attachedRecord?.lifecycle === 'failed' || attachedRecord?.lifecycle === 'interrupted') {\n process.stderr.write(`Runtime ${attachedRecord.runtimeKey} is already ${attachedRecord.lifecycle}. Detached after snapshot.\\n`);\n await cleanup();\n return;\n }\n\n const stopSignals = ['SIGINT', 'SIGTERM', 'SIGHUP'] as const;\n const signalHandlers = stopSignals.map((signal) => {\n const handler = () => {\n void cleanup().finally(() => process.exit(0));\n };\n process.on(signal, handler);\n return { signal, handler };\n });\n\n const unsubscribe = client.onEvent((event: SessionHostEvent) => {\n if (event.sessionId !== runtimeId) return;\n if (event.type === 'session_output') {\n if (event.seq <= lastSeq) return;\n lastSeq = event.seq;\n process.stdout.write(event.data);\n return;\n }\n if (event.type === 'session_exit') {\n void cleanup().finally(() => {\n for (const { signal, handler } of signalHandlers) {\n process.off(signal, handler);\n }\n unsubscribe();\n process.exit(event.exitCode ?? 0);\n });\n }\n });\n\n process.stdout.on('resize', handleResize);\n process.stdin.on('data', handleInput);\n process.stdin.resume();\n if (process.stdin.isTTY) {\n process.stdin.setRawMode(true);\n restoredRawMode = true;\n }\n handleResize();\n if (!effectiveReadOnly) {\n process.stderr.write(`Attached to runtime ${attachedRecord?.runtimeKey || runtimeId}. Press Ctrl+] to detach.\\n`);\n } else {\n process.stderr.write(`Attached to runtime ${attachedRecord?.runtimeKey || runtimeId} (read-only).\\n`);\n }\n await new Promise<void>(() => {});\n } catch (error) {\n await cleanup().catch(() => {});\n throw error;\n }\n}\n\nasync function main(): Promise<void> {\n const { command, positional, readOnly, takeover, showAll } = parseArgs(process.argv.slice(2));\n if (command === 'serve') {\n await runServer();\n return;\n }\n if (command === 'list') {\n await listRuntimes(showAll);\n return;\n }\n if (command === 'attach') {\n const target = positional[0];\n if (!target) {\n throw new Error('runtime target is required: adhdev-sessiond attach <runtimeId|runtimeKey>');\n }\n await attachRuntime(target, readOnly, takeover);\n return;\n }\n if (command === 'resume') {\n const target = positional[0];\n if (!target) {\n throw new Error('runtime target is required: adhdev-sessiond resume <runtimeId|runtimeKey>');\n }\n const client = new SessionHostClient({ endpoint: getDefaultSessionHostEndpoint(SESSION_HOST_APP_NAME) });\n try {\n const listResponse = await client.request<SessionHostRecord[]>({ type: 'list_sessions', payload: {} });\n if (!listResponse.success || !listResponse.result) {\n throw new Error(listResponse.error || 'Failed to list runtimes');\n }\n const runtimeRecord = resolveRuntimeRecord(listResponse.result, target);\n const resumeResponse = await client.request<SessionHostRecord>({\n type: 'resume_session',\n payload: {\n sessionId: runtimeRecord.sessionId,\n },\n });\n if (!resumeResponse.success || !resumeResponse.result) {\n throw new Error(resumeResponse.error || `Failed to resume runtime ${runtimeRecord.runtimeKey}`);\n }\n console.log(`Resumed ${resumeResponse.result.runtimeKey} (${resumeResponse.result.sessionId})`);\n } finally {\n await client.close().catch(() => {});\n }\n return;\n }\n throw new Error(`Unknown command: ${command}`);\n}\n\nif (require.main === module) {\n // Prevent native crashes (e.g. node-pty on Windows) from silently killing the server process\n process.on('uncaughtException', (err) => {\n console.error(`[session-host] Uncaught exception: ${err?.message}\\n${err?.stack || ''}`);\n // Do not exit — keep the server alive for existing sessions\n });\n process.on('unhandledRejection', (reason: any) => {\n console.error(`[session-host] Unhandled rejection: ${reason?.message || reason}`);\n });\n\n void main().catch((error) => {\n console.error(error instanceof Error ? error.message : error);\n process.exit(1);\n });\n}\n","import { EventEmitter } from 'events';\nimport * as fs from 'fs';\nimport * as net from 'net';\nimport {\n SessionHostRegistry,\n createLineParser,\n createResponseEnvelope,\n getDefaultSessionHostEndpoint,\n writeEnvelope,\n} from '@adhdev/session-host-core';\nimport type {\n CreateSessionPayload,\n SessionAttachedClient,\n SessionHostEndpoint,\n SessionHostEvent,\n SessionHostRecord,\n SessionHostRequestEnvelope,\n SessionHostRequest,\n SessionHostResponse,\n} from '@adhdev/session-host-core';\nimport { PtySessionRuntime } from './runtime.js';\nimport { SessionHostStorage, type PersistedRuntimeState } from './storage.js';\n\nexport interface SessionHostServerOptions {\n endpoint?: SessionHostEndpoint;\n appName?: string;\n}\n\nexport class SessionHostServer extends EventEmitter {\n readonly endpoint: SessionHostEndpoint;\n readonly registry = new SessionHostRegistry();\n private runtimes = new Map<string, PtySessionRuntime>();\n private readonly storage: SessionHostStorage;\n private ipcServer: net.Server | null = null;\n private sockets = new Set<net.Socket>();\n private persistTimers = new Map<string, NodeJS.Timeout>();\n\n constructor(options: SessionHostServerOptions = {}) {\n super();\n this.endpoint = options.endpoint || getDefaultSessionHostEndpoint(options.appName || 'adhdev');\n this.storage = new SessionHostStorage({ appName: options.appName || 'adhdev' });\n }\n\n async start(): Promise<void> {\n if (this.endpoint.kind === 'unix') {\n try {\n fs.unlinkSync(this.endpoint.path);\n } catch {\n // noop\n }\n }\n\n this.ipcServer = net.createServer((socket) => {\n this.sockets.add(socket);\n socket.on('close', () => {\n this.sockets.delete(socket);\n });\n socket.on('data', createLineParser((envelope) => {\n if (envelope.kind !== 'request') return;\n void this.handleIncomingRequest(socket, envelope);\n }));\n });\n\n await new Promise<void>((resolve, reject) => {\n this.ipcServer?.once('listening', () => resolve());\n this.ipcServer?.once('error', reject);\n this.ipcServer?.listen(this.endpoint.path);\n });\n\n this.emit('log', `session host endpoint ready: ${this.endpoint.path}`);\n // Do not block readiness on restoring/resuming persisted runtimes.\n // Startup callers only need the IPC endpoint to accept connections.\n setTimeout(() => {\n try {\n this.restorePersistedRuntimes();\n } catch (error: any) {\n this.emit('log', `session host restore failed: ${error?.message || String(error)}`);\n }\n }, 0);\n }\n\n async stop(): Promise<void> {\n this.flushAllPersistence();\n for (const runtime of this.runtimes.values()) {\n try {\n runtime.stop();\n } catch {\n // noop\n }\n }\n this.runtimes.clear();\n for (const timer of this.persistTimers.values()) {\n clearTimeout(timer);\n }\n this.persistTimers.clear();\n for (const socket of this.sockets) {\n socket.destroy();\n }\n this.sockets.clear();\n if (this.ipcServer) {\n const server = this.ipcServer;\n this.ipcServer = null;\n await new Promise<void>((resolve) => server.close(() => resolve()));\n }\n if (this.endpoint.kind === 'unix') {\n try {\n fs.unlinkSync(this.endpoint.path);\n } catch {\n // noop\n }\n }\n this.removeAllListeners();\n }\n\n async handleRequest(request: SessionHostRequest): Promise<SessionHostResponse> {\n try {\n switch (request.type) {\n case 'create_session': {\n const record = this.registry.createSession(request.payload);\n this.schedulePersist(record.sessionId);\n this.emitEvent({ type: 'session_created', sessionId: record.sessionId, record });\n try {\n const startedRecord = this.startRuntime(record, request.payload, 'session_started');\n return { success: true, result: startedRecord };\n } catch (error: any) {\n this.registry.markStopped(record.sessionId, 'failed');\n this.persistNow(record.sessionId);\n return { success: false, error: error?.message || String(error) };\n }\n }\n case 'list_sessions':\n return { success: true, result: this.registry.listSessions() };\n case 'attach_session': {\n const record = this.registry.attachClient(request.payload);\n this.schedulePersist(record.sessionId);\n const client = record.attachedClients.find(item => item.clientId === request.payload.clientId);\n if (client) {\n this.emitEvent({ type: 'client_attached', sessionId: record.sessionId, client });\n }\n return { success: true, result: record };\n }\n case 'detach_session': {\n const record = this.registry.detachClient(request.payload);\n this.schedulePersist(record.sessionId);\n this.emitEvent({ type: 'client_detached', sessionId: record.sessionId, clientId: request.payload.clientId });\n return { success: true, result: record };\n }\n case 'acquire_write': {\n const record = this.registry.acquireWrite(request.payload);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'write_owner_changed', sessionId: record.sessionId, owner: record.writeOwner });\n return { success: true, result: record };\n }\n case 'release_write': {\n const record = this.registry.releaseWrite(request.payload);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'write_owner_changed', sessionId: record.sessionId, owner: record.writeOwner });\n return { success: true, result: record };\n }\n case 'get_snapshot':\n return { success: true, result: this.getSnapshot(request.payload.sessionId, request.payload.sinceSeq) };\n case 'clear_session_buffer': {\n const record = this.registry.clearBuffer(request.payload.sessionId);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'session_cleared', sessionId: record.sessionId });\n return { success: true, result: record };\n }\n case 'update_session_meta': {\n const record = this.registry.updateSessionMeta(\n request.payload.sessionId,\n request.payload.meta || {},\n request.payload.replace === true,\n );\n this.persistNow(record.sessionId);\n return { success: true, result: record };\n }\n case 'send_input': {\n const client = this.getAttachedClient(request.payload.sessionId, request.payload.clientId);\n if (client?.readOnly) {\n return { success: false, error: `Client ${request.payload.clientId} is read-only` };\n }\n const session = this.registry.getSession(request.payload.sessionId);\n if (session?.writeOwner && session.writeOwner.clientId !== request.payload.clientId) {\n return { success: false, error: `Write owned by ${session.writeOwner.clientId}` };\n }\n this.requireRuntime(request.payload.sessionId).write(request.payload.data);\n return { success: true, result: this.registry.getSession(request.payload.sessionId) };\n }\n case 'resize_session': {\n this.requireRuntime(request.payload.sessionId).resize(request.payload.cols, request.payload.rows);\n const record = this.registry.getSession(request.payload.sessionId);\n if (record) {\n this.registry.restoreSession(\n {\n ...record,\n meta: {\n ...(record.meta || {}),\n sessionHostCols: request.payload.cols,\n sessionHostRows: request.payload.rows,\n },\n },\n this.registry.getSnapshot(request.payload.sessionId),\n );\n }\n this.schedulePersist(request.payload.sessionId);\n this.emitEvent({\n type: 'session_resized',\n sessionId: request.payload.sessionId,\n cols: request.payload.cols,\n rows: request.payload.rows,\n });\n return { success: true, result: this.registry.getSession(request.payload.sessionId) };\n }\n case 'stop_session': {\n this.registry.setLifecycle(request.payload.sessionId, 'stopping');\n this.persistNow(request.payload.sessionId);\n this.requireRuntime(request.payload.sessionId).stop();\n this.emitEvent({ type: 'session_stopped', sessionId: request.payload.sessionId });\n return { success: true, result: this.registry.getSession(request.payload.sessionId) };\n }\n case 'resume_session': {\n const existing = this.registry.getSession(request.payload.sessionId);\n if (!existing) {\n return { success: false, error: `Unknown session: ${request.payload.sessionId}` };\n }\n if (this.runtimes.has(request.payload.sessionId)) {\n return { success: true, result: existing };\n }\n const resumed = this.startRuntime(existing, this.buildPayloadFromRecord(existing), 'session_resumed');\n return { success: true, result: resumed };\n }\n default:\n return { success: false, error: `Unsupported session host request: ${(request as { type?: string })?.type || 'unknown'}` };\n }\n } catch (error: any) {\n return { success: false, error: error?.message || String(error) };\n }\n }\n\n private requireRuntime(sessionId: string): PtySessionRuntime {\n const runtime = this.runtimes.get(sessionId);\n if (!runtime) throw new Error(`Runtime not found for session: ${sessionId}`);\n return runtime;\n }\n\n private getAttachedClient(sessionId: string, clientId: string): SessionAttachedClient | null {\n const session = this.registry.getSession(sessionId);\n return session?.attachedClients.find((client) => client.clientId === clientId) || null;\n }\n\n private emitEvent(event: SessionHostEvent): void {\n for (const socket of this.sockets) {\n writeEnvelope(socket, {\n kind: 'event',\n event,\n });\n }\n this.emit('event', event);\n }\n\n private async handleIncomingRequest(socket: net.Socket, envelope: SessionHostRequestEnvelope): Promise<void> {\n const response = await this.handleRequest(envelope.request);\n writeEnvelope(socket, createResponseEnvelope(envelope.requestId, response));\n }\n\n private schedulePersist(sessionId: string): void {\n const existing = this.persistTimers.get(sessionId);\n if (existing) clearTimeout(existing);\n this.persistTimers.set(sessionId, setTimeout(() => {\n this.persistTimers.delete(sessionId);\n this.persistNow(sessionId);\n }, 200));\n }\n\n private persistNow(sessionId: string): void {\n const record = this.registry.getSession(sessionId);\n if (!record) return;\n const snapshot = this.getSnapshot(sessionId);\n this.storage.save(record, snapshot);\n }\n\n private getSnapshot(sessionId: string, sinceSeq?: number) {\n const snapshot = this.registry.getSnapshot(sessionId, sinceSeq);\n const record = this.registry.getSession(sessionId);\n if (typeof sinceSeq === 'number') {\n return {\n ...snapshot,\n cols: typeof record?.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record?.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n };\n }\n\n const runtime = this.runtimes.get(sessionId);\n const runtimeText = runtime?.getSnapshotText?.() || '';\n if (!runtimeText) {\n return {\n ...snapshot,\n cols: typeof record?.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record?.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n };\n }\n\n return {\n ...snapshot,\n text: runtimeText,\n truncated: false,\n cols: typeof record?.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record?.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n };\n }\n\n flushAllPersistence(): void {\n for (const sessionId of this.runtimes.keys()) {\n this.persistNow(sessionId);\n }\n for (const record of this.registry.listSessions()) {\n this.persistNow(record.sessionId);\n }\n }\n\n private restorePersistedRuntimes(): void {\n const states = this.storage.loadAll();\n const runtimesToResume: Array<{\n persisted: PersistedRuntimeState;\n recoveredRecord: SessionHostRecord;\n }> = [];\n let skippedOrphanLiveSessions = 0;\n for (const persisted of states) {\n const wasLiveRuntime = !['stopped', 'failed'].includes(persisted.record.lifecycle);\n const hadAttachedClients = Array.isArray(persisted.record.attachedClients) && persisted.record.attachedClients.length > 0;\n const hadWriteOwner = !!persisted.record.writeOwner;\n const shouldAutoResume = wasLiveRuntime && (hadAttachedClients || hadWriteOwner);\n const recoveredRecord: SessionHostRecord = {\n ...persisted.record,\n attachedClients: [],\n writeOwner: null,\n lifecycle: shouldAutoResume ? 'interrupted' : (wasLiveRuntime ? 'stopped' : persisted.record.lifecycle),\n lastActivityAt: Date.now(),\n meta: {\n ...(persisted.record.meta || {}),\n restoredFromStorage: true,\n runtimeRecoveryState: shouldAutoResume\n ? 'host_restart_interrupted'\n : (wasLiveRuntime ? 'orphan_snapshot' : 'snapshot'),\n runtimeHadAttachedClientsAtCrash: hadAttachedClients,\n runtimeHadWriteOwnerAtCrash: hadWriteOwner,\n },\n };\n this.registry.restoreSession(recoveredRecord, persisted.snapshot);\n this.storage.save(recoveredRecord, persisted.snapshot);\n if (shouldAutoResume) {\n runtimesToResume.push({ persisted, recoveredRecord });\n } else if (wasLiveRuntime) {\n skippedOrphanLiveSessions += 1;\n }\n }\n\n if (skippedOrphanLiveSessions > 0) {\n this.emit('log', `session host skipped ${skippedOrphanLiveSessions} orphan live runtime(s) during restore`);\n }\n\n for (const { persisted, recoveredRecord } of runtimesToResume) {\n try {\n const resumed = this.startRuntime(\n recoveredRecord,\n this.buildPayloadFromRecord(recoveredRecord),\n 'session_resumed',\n );\n const resumedMeta = {\n ...(resumed.meta || {}),\n restoredFromStorage: true,\n runtimeRecoveryState: 'auto_resumed',\n };\n this.registry.restoreSession(\n { ...resumed, meta: resumedMeta },\n this.registry.getSnapshot(resumed.sessionId),\n );\n this.persistNow(resumed.sessionId);\n } catch (error: any) {\n const interrupted = this.registry.setLifecycle(recoveredRecord.sessionId, 'interrupted');\n this.registry.restoreSession({\n ...interrupted,\n meta: {\n ...(interrupted.meta || {}),\n restoredFromStorage: true,\n runtimeRecoveryState: 'resume_failed',\n runtimeRecoveryError: error?.message || String(error),\n },\n }, persisted.snapshot);\n this.persistNow(recoveredRecord.sessionId);\n }\n }\n }\n\n private buildPayloadFromRecord(record: SessionHostRecord): CreateSessionPayload {\n return {\n sessionId: record.sessionId,\n runtimeKey: record.runtimeKey,\n displayName: record.displayName,\n providerType: record.providerType,\n category: record.category,\n workspace: record.workspace,\n launchCommand: record.launchCommand,\n cols: typeof record.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n meta: record.meta,\n };\n }\n\n private startRuntime(\n record: SessionHostRecord,\n payload: CreateSessionPayload,\n startEventType: 'session_started' | 'session_resumed',\n ): SessionHostRecord {\n const runtime = new PtySessionRuntime({\n sessionId: record.sessionId,\n payload,\n onData: (data) => {\n const { seq } = this.registry.appendOutput(record.sessionId, data);\n this.schedulePersist(record.sessionId);\n this.emitEvent({ type: 'session_output', sessionId: record.sessionId, seq, data });\n },\n onExit: (exitCode) => {\n this.registry.markStopped(record.sessionId, exitCode === 0 ? 'stopped' : 'failed');\n this.runtimes.delete(record.sessionId);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'session_exit', sessionId: record.sessionId, exitCode });\n // Clean up persistence file after a brief delay (allow post-mortem reads)\n setTimeout(() => this.storage.remove(record.sessionId), 5_000);\n },\n });\n\n this.registry.setLifecycle(record.sessionId, 'starting');\n const pid = runtime.start();\n this.runtimes.set(record.sessionId, runtime);\n const startedRecord = this.registry.markStarted(record.sessionId, pid);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: startEventType, sessionId: record.sessionId, pid });\n return startedRecord;\n }\n}\n","import * as fs from 'fs';\nimport * as os from 'os';\nimport * as path from 'path';\nimport * as pty from 'node-pty';\nimport type { IPty } from 'node-pty';\nimport type { CreateSessionPayload } from '@adhdev/session-host-core';\n\ntype TerminalMirrorHandle = {\n write(data: string | Uint8Array): void;\n resize(cols: number, rows: number): void;\n formatVT(): string;\n getCursorPosition(): { col: number; row: number };\n dispose(): void;\n};\n\ntype GhosttyTerminalHandle = {\n write(data: string | Uint8Array): void;\n resize(cols: number, rows: number): void;\n formatVT(): string;\n getCursorPosition(): { col: number; row: number };\n dispose(): void;\n};\n\ntype GhosttyBinding = {\n createTerminal(options: { cols: number; rows: number; scrollback: number }): GhosttyTerminalHandle;\n};\n\ntype XtermBufferLine = {\n translateToString(trimRight?: boolean): string;\n};\n\ntype XtermBuffer = {\n length: number;\n viewportY: number;\n cursorX?: number;\n cursorY?: number;\n getLine(index: number): XtermBufferLine | undefined;\n};\n\ntype XtermTerminal = {\n buffer: { active: XtermBuffer };\n write(data: string, callback?: () => void): void;\n resize(cols: number, rows: number): void;\n dispose(): void;\n};\n\ntype XtermCtor = new (options: { cols: number; rows: number; scrollback: number }) => XtermTerminal;\n\nlet terminalMirrorFactory:\n | ((options: { cols: number; rows: number; scrollback: number }) => TerminalMirrorHandle)\n | null\n | undefined;\nlet terminalMirrorWarning: string | null = null;\n\nif (os.platform() !== 'win32') {\n try {\n const fs = require('fs');\n const ptyDir = path.resolve(path.dirname(require.resolve('node-pty')), '..');\n const platformArch = `${os.platform()}-${os.arch()}`;\n const helper = path.join(ptyDir, 'prebuilds', platformArch, 'spawn-helper');\n if (fs.existsSync(helper)) {\n const stat = fs.statSync(helper);\n if (!(stat.mode & 0o111)) {\n fs.chmodSync(helper, stat.mode | 0o755);\n }\n }\n } catch {\n // best-effort: node-pty still works on most installs without this\n }\n}\n\nexport interface PtyRuntimeOptions {\n sessionId: string;\n payload: CreateSessionPayload;\n onData: (data: string) => void;\n onExit: (exitCode: number | null) => void;\n}\n\nfunction buildRuntimeEnv(baseEnv: NodeJS.ProcessEnv, launchEnv?: Record<string, string>): Record<string, string> {\n const env: Record<string, string> = {};\n const source = { ...baseEnv, ...(launchEnv || {}) } as NodeJS.ProcessEnv;\n\n for (const [key, value] of Object.entries(source)) {\n if (typeof value !== 'string') continue;\n env[key] = value;\n }\n\n for (const key of Object.keys(env)) {\n if (\n key === 'INIT_CWD'\n || key === 'npm_command'\n || key === 'npm_execpath'\n || key === 'npm_node_execpath'\n || key.startsWith('npm_')\n || key.startsWith('npm_config_')\n || key.startsWith('npm_package_')\n || key.startsWith('npm_lifecycle_')\n || key.startsWith('PNPM_')\n || key.startsWith('YARN_')\n || key.startsWith('BUN_')\n ) {\n delete env[key];\n }\n }\n\n if (!env.NO_COLOR) {\n if (!env.TERM || env.TERM === 'xterm-color') env.TERM = 'xterm-256color';\n if (!env.COLORTERM) env.COLORTERM = 'truecolor';\n if (process.platform === 'win32') {\n if (!env.FORCE_COLOR) env.FORCE_COLOR = '1';\n if (!env.CLICOLOR) env.CLICOLOR = '1';\n }\n }\n\n return env;\n}\n\nfunction computeTerminalQueryTail(buffer: string): string {\n const prefixes = ['\\x1b[6n', '\\x1b[?6n'];\n const maxLength = prefixes.reduce((n, value) => Math.max(n, value.length), 0) - 1;\n const start = Math.max(0, buffer.length - maxLength);\n for (let i = start; i < buffer.length; i++) {\n const suffix = buffer.slice(i);\n if (prefixes.some((pattern) => suffix.length < pattern.length && pattern.startsWith(suffix))) {\n return suffix;\n }\n }\n return '';\n}\n\nfunction formatXtermViewport(terminal: XtermTerminal, rows: number): string {\n const buffer = terminal.buffer.active;\n const start = Math.max(0, buffer.viewportY || 0);\n const end = Math.max(start, Math.min(buffer.length || 0, start + Math.max(1, rows | 0)));\n const lines: string[] = [];\n\n for (let i = start; i < end; i++) {\n const line = buffer.getLine(i);\n lines.push(line ? line.translateToString(true) : '');\n }\n\n let first = 0;\n let last = lines.length;\n while (first < last && !lines[first]?.trim()) first++;\n while (last > first && !lines[last - 1]?.trim()) last--;\n return lines.slice(first, last).join('\\n');\n}\n\nfunction createXtermMirror(options: { cols: number; rows: number; scrollback: number }): TerminalMirrorHandle {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const mod = require('@xterm/xterm');\n const Terminal = (mod.Terminal || mod.default?.Terminal || mod.default) as XtermCtor | undefined;\n if (!Terminal) {\n throw new Error('@xterm/xterm Terminal export not found');\n }\n\n let currentRows = Math.max(1, options.rows | 0);\n const terminal = new Terminal({\n cols: Math.max(1, options.cols | 0),\n rows: currentRows,\n scrollback: Math.max(0, options.scrollback | 0),\n });\n\n return {\n write(data: string | Uint8Array): void {\n if (!data) return;\n terminal.write(typeof data === 'string' ? data : Buffer.from(data).toString('utf8'));\n },\n resize(cols: number, rows: number): void {\n currentRows = Math.max(1, rows | 0);\n terminal.resize(Math.max(1, cols | 0), currentRows);\n },\n formatVT(): string {\n return formatXtermViewport(terminal, currentRows);\n },\n getCursorPosition(): { col: number; row: number } {\n const buffer = terminal.buffer.active;\n return {\n col: Math.max(0, buffer.cursorX || 0),\n row: Math.max(0, buffer.cursorY || 0),\n };\n },\n dispose(): void {\n terminal.dispose();\n },\n };\n}\n\nfunction normalizeGhosttyBinding(mod: any): GhosttyBinding | null {\n const raw = mod?.default?.createTerminal ? mod.default : mod?.createTerminal ? mod : null;\n if (!raw) return null;\n\n // Wrap the native handle to fill in any missing methods (pre-built binaries may lack\n // formatVT / getCursorPosition if built before those methods were added to the Rust side).\n return {\n createTerminal(options: { cols: number; rows: number; scrollback: number }): GhosttyTerminalHandle {\n const handle = raw.createTerminal(options) as any;\n return {\n write(data: string | Uint8Array): void { handle.write(data); },\n resize(cols: number, rows: number): void { handle.resize(cols, rows); },\n formatVT(): string {\n if (typeof handle.formatVT === 'function') return handle.formatVT();\n // Fallback: formatPlainText is always available in the shipped bindings\n if (typeof handle.formatPlainText === 'function') return handle.formatPlainText({ trim: false }) as string;\n return '';\n },\n getCursorPosition(): { col: number; row: number } {\n if (typeof handle.getCursorPosition === 'function') return handle.getCursorPosition() as { col: number; row: number };\n return { col: 0, row: 0 };\n },\n dispose(): void { handle.dispose(); },\n };\n },\n };\n}\n\nfunction getTerminalMirrorFactory(): (options: { cols: number; rows: number; scrollback: number }) => TerminalMirrorHandle {\n if (terminalMirrorFactory) return terminalMirrorFactory;\n if (terminalMirrorFactory === null) {\n throw new Error(terminalMirrorWarning || 'No terminal mirror backend available');\n }\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const ghosttyMod = require('@adhdev/ghostty-vt-node');\n const binding = normalizeGhosttyBinding(ghosttyMod);\n if (!binding) {\n throw new Error('@adhdev/ghostty-vt-node does not export createTerminal()');\n }\n terminalMirrorFactory = (options) => binding.createTerminal(options);\n return terminalMirrorFactory;\n } catch (ghosttyError: any) {\n try {\n terminalMirrorFactory = createXtermMirror;\n terminalMirrorWarning = `Ghostty VT unavailable; falling back to xterm mirror (${ghosttyError?.message || String(ghosttyError)})`;\n return terminalMirrorFactory;\n } catch (xtermError: any) {\n terminalMirrorFactory = null;\n terminalMirrorWarning = `No terminal mirror backend available (ghostty: ${ghosttyError?.message || String(ghosttyError)}; xterm: ${xtermError?.message || String(xtermError)})`;\n throw new Error(terminalMirrorWarning);\n }\n }\n}\n\nexport class PtySessionRuntime {\n readonly sessionId: string;\n readonly payload: CreateSessionPayload;\n readonly cols: number;\n readonly rows: number;\n\n private ptyProcess: IPty | null = null;\n private screenMirror: TerminalMirrorHandle | null = null;\n private pendingQueryScanTail = '';\n private onDataCallback: (data: string) => void;\n private onExitCallback: (exitCode: number | null) => void;\n\n constructor(options: PtyRuntimeOptions) {\n this.sessionId = options.sessionId;\n this.payload = options.payload;\n this.cols = options.payload.cols || 80;\n this.rows = options.payload.rows || 24;\n this.onDataCallback = options.onData;\n this.onExitCallback = options.onExit;\n }\n\n start(): number {\n if (this.ptyProcess) return this.ptyProcess.pid;\n\n const command = this.payload.launchCommand.command;\n const args = this.payload.launchCommand.args || [];\n const env = buildRuntimeEnv(process.env, this.payload.launchCommand.env);\n\n // Validate workspace directory — an invalid cwd causes a native crash on Windows\n // (node-pty error code 267: ERROR_DIRECTORY) that bypasses JS try/catch\n let cwd = this.payload.workspace || process.cwd();\n if (cwd) {\n try {\n const stat = fs.statSync(cwd);\n if (!stat.isDirectory()) cwd = os.homedir();\n } catch {\n cwd = os.homedir();\n }\n }\n\n this.ptyProcess = pty.spawn(command, args, {\n name: 'xterm-256color',\n cols: this.cols,\n rows: this.rows,\n cwd,\n env,\n });\n this.screenMirror = getTerminalMirrorFactory()({\n cols: this.cols,\n rows: this.rows,\n scrollback: 32768,\n });\n\n this.ptyProcess.onData((data: string) => {\n this.screenMirror?.write(data);\n this.respondToTerminalQueries(data);\n this.onDataCallback(data);\n });\n\n this.ptyProcess.onExit(({ exitCode }) => {\n this.ptyProcess = null;\n this.screenMirror?.dispose();\n this.screenMirror = null;\n this.pendingQueryScanTail = '';\n this.onExitCallback(exitCode ?? null);\n });\n\n return this.ptyProcess.pid;\n }\n\n write(data: string): void {\n if (!this.ptyProcess) throw new Error(`Session not running: ${this.sessionId}`);\n this.ptyProcess.write(data);\n }\n\n resize(cols: number, rows: number): void {\n if (!this.ptyProcess) throw new Error(`Session not running: ${this.sessionId}`);\n this.ptyProcess.resize(cols, rows);\n this.screenMirror?.resize(cols, rows);\n }\n\n stop(): void {\n if (!this.ptyProcess) return;\n this.ptyProcess.kill();\n }\n\n getSnapshotText(): string {\n return this.screenMirror?.formatVT() || '';\n }\n\n private respondToTerminalQueries(data: string): void {\n if (!this.ptyProcess || !this.screenMirror || !data) return;\n\n const combined = this.pendingQueryScanTail + data;\n const regex = /\\x1b\\[(\\?)?6n/g;\n let match: RegExpExecArray | null;\n\n while ((match = regex.exec(combined)) !== null) {\n const cursor = this.screenMirror.getCursorPosition();\n const row = Math.max(1, (cursor.row | 0) + 1);\n const col = Math.max(1, (cursor.col | 0) + 1);\n const response = match[1]\n ? `\\x1b[?${row};${col}R`\n : `\\x1b[${row};${col}R`;\n this.ptyProcess.write(response);\n }\n\n this.pendingQueryScanTail = computeTerminalQueryTail(combined);\n }\n}\n","import * as fs from 'fs';\nimport * as os from 'os';\nimport * as path from 'path';\nimport type { SessionBufferSnapshot, SessionHostRecord } from '@adhdev/session-host-core';\n\nexport interface PersistedRuntimeState {\n record: SessionHostRecord;\n snapshot: SessionBufferSnapshot;\n updatedAt: number;\n}\n\ninterface SessionHostStorageOptions {\n appName?: string;\n}\n\nexport class SessionHostStorage {\n private readonly rootDir: string;\n private readonly runtimesDir: string;\n\n constructor(options: SessionHostStorageOptions = {}) {\n const appName = options.appName || 'adhdev';\n this.rootDir = path.join(os.homedir(), '.adhdev', 'session-host', appName);\n this.runtimesDir = path.join(this.rootDir, 'runtimes');\n }\n\n loadAll(): PersistedRuntimeState[] {\n if (!fs.existsSync(this.runtimesDir)) return [];\n const entries = fs.readdirSync(this.runtimesDir, { withFileTypes: true });\n const states: PersistedRuntimeState[] = [];\n for (const entry of entries) {\n if (!entry.isFile() || !entry.name.endsWith('.json')) continue;\n const fullPath = path.join(this.runtimesDir, entry.name);\n try {\n const parsed = JSON.parse(fs.readFileSync(fullPath, 'utf8')) as PersistedRuntimeState;\n if (parsed?.record?.sessionId) {\n states.push(parsed);\n }\n } catch {\n // Ignore malformed snapshots; host should still boot.\n }\n }\n return states.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));\n }\n\n save(record: SessionHostRecord, snapshot: SessionBufferSnapshot): void {\n fs.mkdirSync(this.runtimesDir, { recursive: true });\n const filePath = path.join(this.runtimesDir, `${record.sessionId}.json`);\n const payload: PersistedRuntimeState = {\n record,\n snapshot,\n updatedAt: Date.now(),\n };\n fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), 'utf8');\n }\n\n remove(sessionId: string): void {\n const filePath = path.join(this.runtimesDir, `${sessionId}.json`);\n try {\n fs.unlinkSync(filePath);\n } catch {\n // File may not exist — ignore.\n }\n }\n}\n"],"mappings":";;;;;;;;;AAEA,SAAS,kBAAkB;AAC3B,YAAYA,SAAQ;AACpB,YAAYC,SAAQ;AACpB,YAAYC,WAAU;AACtB;AAAA,EACE;AAAA,EACA;AAAA,EACA,iCAAAC;AAAA,EACA;AAAA,OAGK;;;ACbP,SAAS,oBAAoB;AAC7B,YAAYC,SAAQ;AACpB,YAAY,SAAS;AACrB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACTP,YAAY,QAAQ;AACpB,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,YAAY,SAAS;AA6CrB,IAAI;AAIJ,IAAI,wBAAuC;AAE3C,IAAO,YAAS,MAAM,SAAS;AAC7B,MAAI;AACF,UAAMC,MAAK,UAAQ,IAAI;AACvB,UAAM,SAAc,aAAa,aAAQ,UAAQ,QAAQ,UAAU,CAAC,GAAG,IAAI;AAC3E,UAAM,eAAe,GAAM,YAAS,CAAC,IAAO,QAA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D,UAAQ,GAAG,sBAAsB,CAAC,WAAgB;AAChD,YAAQ,MAAM,uCAAuC,QAAQ,WAAW,MAAM,EAAE;AAAA,EAClF,CAAC;AAED,OAAK,KAAK,EAAE,MAAM,CAAC,UAAU;AAC3B,YAAQ,MAAM,iBAAiB,QAAQ,MAAM,UAAU,KAAK;AAC5D,YAAQ,KAAK,CAAC;AAAA,EAChB,CAAC;AACH;","names":["fs","os","path","getDefaultSessionHostEndpoint","fs","fs","fs","os","path","resolve","getDefaultSessionHostEndpoint"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/server.ts","../src/runtime.ts","../src/storage.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { randomUUID } from 'crypto';\nimport * as fs from 'fs';\nimport * as os from 'os';\nimport * as path from 'path';\nimport {\n SessionHostClient,\n formatRuntimeOwner,\n getDefaultSessionHostEndpoint,\n resolveRuntimeRecord,\n type SessionHostEvent,\n type SessionHostRecord,\n} from '@adhdev/session-host-core';\nimport { SessionHostServer } from './server.js';\n\nexport { SessionHostServer } from './server.js';\nexport type { SessionHostServerOptions } from './server.js';\n\nconst SESSION_HOST_APP_NAME = process.env.ADHDEV_SESSION_HOST_NAME || 'adhdev';\n\nfunction getSessionHostPidFile(appName: string): string {\n const dir = path.join(os.homedir(), '.adhdev');\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });\n return path.join(dir, `${appName}-session-host.pid`);\n}\n\nfunction writeSessionHostPid(appName: string): void {\n fs.writeFileSync(getSessionHostPidFile(appName), String(process.pid), 'utf8');\n}\n\nfunction removeSessionHostPid(appName: string): void {\n try {\n fs.unlinkSync(getSessionHostPidFile(appName));\n } catch {\n // noop\n }\n}\n\nfunction parseArgs(argv: string[]) {\n const [command, ...rest] = argv;\n const readOnly = rest.includes('--read-only');\n const takeover = rest.includes('--takeover');\n const showAll = rest.includes('--all');\n const positional = rest.filter((arg) => arg !== '--read-only' && arg !== '--takeover' && arg !== '--all');\n return {\n command: command || 'serve',\n positional,\n readOnly,\n takeover,\n showAll,\n };\n}\n\nasync function runServer(): Promise<void> {\n const server = new SessionHostServer({ appName: SESSION_HOST_APP_NAME });\n writeSessionHostPid(SESSION_HOST_APP_NAME);\n await server.start();\n\n process.on('SIGINT', async () => {\n await server.stop();\n removeSessionHostPid(SESSION_HOST_APP_NAME);\n process.exit(0);\n });\n\n process.on('SIGTERM', async () => {\n await server.stop();\n removeSessionHostPid(SESSION_HOST_APP_NAME);\n process.exit(0);\n });\n\n // Fallback: flush persistence on any exit (covers Windows where SIGTERM is unsupported)\n process.on('exit', () => {\n server.flushAllPersistence();\n removeSessionHostPid(SESSION_HOST_APP_NAME);\n });\n\n // Keep the host alive; IPC transport wiring comes next.\n await new Promise<void>(() => {});\n}\n\nasync function listRuntimes(showAll = false): Promise<void> {\n const client = new SessionHostClient({ endpoint: getDefaultSessionHostEndpoint(SESSION_HOST_APP_NAME) });\n try {\n const response = await client.request<SessionHostRecord[]>({\n type: 'list_sessions',\n payload: {},\n });\n if (!response.success) {\n throw new Error(response.error || 'Failed to list runtimes');\n }\n const runtimes = (response.result || []).filter((runtime: SessionHostRecord) => showAll || runtime.lifecycle !== 'stopped');\n if (runtimes.length === 0) {\n console.log('No runtimes.');\n return;\n }\n console.log('runtimeKey\\tlifecycle\\towner\\tworkspace\\tid\\tdisplayName');\n for (const runtime of runtimes) {\n console.log([\n runtime.runtimeKey,\n runtime.lifecycle,\n formatRuntimeOwner(runtime),\n runtime.workspaceLabel,\n runtime.sessionId,\n runtime.displayName,\n ].join('\\t'));\n }\n } finally {\n await client.close().catch(() => {});\n }\n}\n\nasync function attachRuntime(target: string, readOnly = false, takeover = false): Promise<void> {\n const client = new SessionHostClient({ endpoint: getDefaultSessionHostEndpoint(SESSION_HOST_APP_NAME) });\n const clientId = `local-terminal-${process.pid}-${randomUUID().slice(0, 8)}`;\n let lastSeq = 0;\n let restoredRawMode = false;\n let runtimeId = '';\n let localReadOnly = readOnly;\n\n const cleanup = async () => {\n process.stdout.off('resize', handleResize);\n process.stdin.off('data', handleInput);\n process.stdin.pause();\n if (process.stdin.isTTY && restoredRawMode) {\n process.stdin.setRawMode(false);\n }\n await client.request({\n type: 'release_write',\n payload: {\n sessionId: runtimeId,\n clientId,\n },\n }).catch(() => ({ success: false }));\n await client.request({\n type: 'detach_session',\n payload: {\n sessionId: runtimeId,\n clientId,\n },\n }).catch(() => ({ success: false }));\n await client.close().catch(() => {});\n };\n\n const handleResize = () => {\n void client.request({\n type: 'resize_session',\n payload: {\n sessionId: runtimeId,\n cols: process.stdout.columns || 80,\n rows: process.stdout.rows || 24,\n },\n }).catch(() => ({ success: false }));\n };\n\n const sendInputWithTakeover = async (data: string) => {\n let response = await client.request({\n type: 'send_input',\n payload: {\n sessionId: runtimeId,\n clientId,\n data,\n },\n });\n if (!response.success && response.error?.startsWith('Write owned by ')) {\n const ownerResponse = await client.request<SessionHostRecord>({\n type: 'acquire_write',\n payload: {\n sessionId: runtimeId,\n clientId,\n ownerType: 'user',\n force: true,\n },\n });\n if (ownerResponse.success && ownerResponse.result) {\n response = await client.request({\n type: 'send_input',\n payload: {\n sessionId: runtimeId,\n clientId,\n data,\n },\n });\n if (response.success) {\n process.stderr.write(`Took control of ${ownerResponse.result.runtimeKey}.\\n`);\n }\n }\n }\n return response;\n };\n\n const handleInput = (chunk: Buffer) => {\n if (!localReadOnly && chunk.length === 1 && chunk[0] === 0x1d) {\n void cleanup().finally(() => process.exit(0));\n return;\n }\n if (localReadOnly) return;\n void sendInputWithTakeover(chunk.toString('utf8')).catch(() => ({ success: false }));\n };\n\n try {\n if (readOnly && takeover) {\n throw new Error('Use either --read-only or --takeover, not both');\n }\n\n const listResponse = await client.request<SessionHostRecord[]>({\n type: 'list_sessions',\n payload: {},\n });\n if (!listResponse.success || !listResponse.result) {\n throw new Error(listResponse.error || 'Failed to list runtimes');\n }\n let runtimeRecord = resolveRuntimeRecord(listResponse.result, target);\n runtimeId = runtimeRecord.sessionId;\n\n if (runtimeRecord.lifecycle === 'interrupted' && !readOnly) {\n const resumeResponse = await client.request<SessionHostRecord>({\n type: 'resume_session',\n payload: {\n sessionId: runtimeId,\n },\n });\n if (resumeResponse.success && resumeResponse.result) {\n runtimeRecord = resumeResponse.result;\n } else {\n process.stderr.write(\n `Runtime ${runtimeRecord.runtimeKey} could not be resumed automatically: ${resumeResponse.error || 'unknown error'}\\n`,\n );\n }\n }\n\n let effectiveReadOnly = readOnly;\n if (!effectiveReadOnly && runtimeRecord.writeOwner && runtimeRecord.writeOwner.clientId !== clientId && !takeover) {\n process.stderr.write(\n `Runtime ${runtimeRecord.runtimeKey} is currently owned by ${runtimeRecord.writeOwner.clientId}; first input will take control here.\\n`,\n );\n }\n localReadOnly = effectiveReadOnly;\n\n const attachResponse = await client.request<SessionHostRecord>({\n type: 'attach_session',\n payload: {\n sessionId: runtimeId,\n clientId,\n clientType: 'local-terminal',\n readOnly: effectiveReadOnly,\n },\n });\n if (!attachResponse.success) {\n throw new Error(attachResponse.error || `Failed to attach runtime ${runtimeId}`);\n }\n const attachedRecord = attachResponse.result || null;\n\n if (!effectiveReadOnly && takeover) {\n const ownerResponse = await client.request<SessionHostRecord>({\n type: 'acquire_write',\n payload: {\n sessionId: runtimeId,\n clientId,\n ownerType: 'user',\n force: takeover,\n },\n });\n if (!ownerResponse.success) {\n throw new Error(ownerResponse.error || `Failed to acquire write owner for runtime ${runtimeId}`);\n }\n }\n\n const snapshotResponse = await client.request<{ seq: number; text: string; truncated: boolean; cols?: number; rows?: number }>({\n type: 'get_snapshot',\n payload: { sessionId: runtimeId },\n });\n if (!snapshotResponse.success) {\n throw new Error(snapshotResponse.error || `Failed to read runtime snapshot ${runtimeId}`);\n }\n lastSeq = snapshotResponse.result?.seq || 0;\n if (snapshotResponse.result?.text) {\n process.stdout.write(snapshotResponse.result.text);\n }\n if (attachedRecord?.lifecycle === 'stopped' || attachedRecord?.lifecycle === 'failed' || attachedRecord?.lifecycle === 'interrupted') {\n process.stderr.write(`Runtime ${attachedRecord.runtimeKey} is already ${attachedRecord.lifecycle}. Detached after snapshot.\\n`);\n await cleanup();\n return;\n }\n\n const stopSignals = ['SIGINT', 'SIGTERM', 'SIGHUP'] as const;\n const signalHandlers = stopSignals.map((signal) => {\n const handler = () => {\n void cleanup().finally(() => process.exit(0));\n };\n process.on(signal, handler);\n return { signal, handler };\n });\n\n const unsubscribe = client.onEvent((event: SessionHostEvent) => {\n if (event.sessionId !== runtimeId) return;\n if (event.type === 'session_output') {\n if (event.seq <= lastSeq) return;\n lastSeq = event.seq;\n process.stdout.write(event.data);\n return;\n }\n if (event.type === 'session_exit') {\n void cleanup().finally(() => {\n for (const { signal, handler } of signalHandlers) {\n process.off(signal, handler);\n }\n unsubscribe();\n process.exit(event.exitCode ?? 0);\n });\n }\n });\n\n process.stdout.on('resize', handleResize);\n process.stdin.on('data', handleInput);\n process.stdin.resume();\n if (process.stdin.isTTY) {\n process.stdin.setRawMode(true);\n restoredRawMode = true;\n }\n handleResize();\n if (!effectiveReadOnly) {\n process.stderr.write(`Attached to runtime ${attachedRecord?.runtimeKey || runtimeId}. Press Ctrl+] to detach.\\n`);\n } else {\n process.stderr.write(`Attached to runtime ${attachedRecord?.runtimeKey || runtimeId} (read-only).\\n`);\n }\n await new Promise<void>(() => {});\n } catch (error) {\n await cleanup().catch(() => {});\n throw error;\n }\n}\n\nasync function main(): Promise<void> {\n const { command, positional, readOnly, takeover, showAll } = parseArgs(process.argv.slice(2));\n if (command === 'serve') {\n await runServer();\n return;\n }\n if (command === 'list') {\n await listRuntimes(showAll);\n return;\n }\n if (command === 'attach') {\n const target = positional[0];\n if (!target) {\n throw new Error('runtime target is required: adhdev-sessiond attach <runtimeId|runtimeKey>');\n }\n await attachRuntime(target, readOnly, takeover);\n return;\n }\n if (command === 'resume') {\n const target = positional[0];\n if (!target) {\n throw new Error('runtime target is required: adhdev-sessiond resume <runtimeId|runtimeKey>');\n }\n const client = new SessionHostClient({ endpoint: getDefaultSessionHostEndpoint(SESSION_HOST_APP_NAME) });\n try {\n const listResponse = await client.request<SessionHostRecord[]>({ type: 'list_sessions', payload: {} });\n if (!listResponse.success || !listResponse.result) {\n throw new Error(listResponse.error || 'Failed to list runtimes');\n }\n const runtimeRecord = resolveRuntimeRecord(listResponse.result, target);\n const resumeResponse = await client.request<SessionHostRecord>({\n type: 'resume_session',\n payload: {\n sessionId: runtimeRecord.sessionId,\n },\n });\n if (!resumeResponse.success || !resumeResponse.result) {\n throw new Error(resumeResponse.error || `Failed to resume runtime ${runtimeRecord.runtimeKey}`);\n }\n console.log(`Resumed ${resumeResponse.result.runtimeKey} (${resumeResponse.result.sessionId})`);\n } finally {\n await client.close().catch(() => {});\n }\n return;\n }\n throw new Error(`Unknown command: ${command}`);\n}\n\nif (require.main === module) {\n // Prevent native crashes (e.g. node-pty on Windows) from silently killing the server process\n process.on('uncaughtException', (err) => {\n console.error(`[session-host] Uncaught exception: ${err?.message}\\n${err?.stack || ''}`);\n // Do not exit — keep the server alive for existing sessions\n });\n process.on('unhandledRejection', (reason: any) => {\n console.error(`[session-host] Unhandled rejection: ${reason?.message || reason}`);\n });\n\n void main().catch((error) => {\n console.error(error instanceof Error ? error.message : error);\n process.exit(1);\n });\n}\n","import { EventEmitter } from 'events';\nimport * as fs from 'fs';\nimport * as net from 'net';\nimport {\n SessionHostRegistry,\n createLineParser,\n createResponseEnvelope,\n getDefaultSessionHostEndpoint,\n writeEnvelope,\n} from '@adhdev/session-host-core';\nimport type {\n CreateSessionPayload,\n SessionAttachedClient,\n SessionHostEndpoint,\n SessionHostEvent,\n SessionHostRecord,\n SessionHostRequestEnvelope,\n SessionHostRequest,\n SessionHostResponse,\n} from '@adhdev/session-host-core';\nimport { PtySessionRuntime } from './runtime.js';\nimport { SessionHostStorage, type PersistedRuntimeState } from './storage.js';\n\nexport interface SessionHostServerOptions {\n endpoint?: SessionHostEndpoint;\n appName?: string;\n}\n\nexport class SessionHostServer extends EventEmitter {\n readonly endpoint: SessionHostEndpoint;\n readonly registry = new SessionHostRegistry();\n private runtimes = new Map<string, PtySessionRuntime>();\n private readonly storage: SessionHostStorage;\n private ipcServer: net.Server | null = null;\n private sockets = new Set<net.Socket>();\n private persistTimers = new Map<string, NodeJS.Timeout>();\n\n constructor(options: SessionHostServerOptions = {}) {\n super();\n this.endpoint = options.endpoint || getDefaultSessionHostEndpoint(options.appName || 'adhdev');\n this.storage = new SessionHostStorage({ appName: options.appName || 'adhdev' });\n }\n\n async start(): Promise<void> {\n if (this.endpoint.kind === 'unix') {\n try {\n fs.unlinkSync(this.endpoint.path);\n } catch {\n // noop\n }\n }\n\n this.ipcServer = net.createServer((socket) => {\n this.sockets.add(socket);\n socket.on('close', () => {\n this.sockets.delete(socket);\n });\n socket.on('data', createLineParser((envelope) => {\n if (envelope.kind !== 'request') return;\n void this.handleIncomingRequest(socket, envelope);\n }));\n });\n\n await new Promise<void>((resolve, reject) => {\n this.ipcServer?.once('listening', () => resolve());\n this.ipcServer?.once('error', reject);\n this.ipcServer?.listen(this.endpoint.path);\n });\n\n this.emit('log', `session host endpoint ready: ${this.endpoint.path}`);\n // Do not block readiness on restoring/resuming persisted runtimes.\n // Startup callers only need the IPC endpoint to accept connections.\n setTimeout(() => {\n try {\n this.restorePersistedRuntimes();\n } catch (error: any) {\n this.emit('log', `session host restore failed: ${error?.message || String(error)}`);\n }\n }, 0);\n }\n\n async stop(): Promise<void> {\n this.flushAllPersistence();\n for (const runtime of this.runtimes.values()) {\n try {\n runtime.stop();\n } catch {\n // noop\n }\n }\n this.runtimes.clear();\n for (const timer of this.persistTimers.values()) {\n clearTimeout(timer);\n }\n this.persistTimers.clear();\n for (const socket of this.sockets) {\n socket.destroy();\n }\n this.sockets.clear();\n if (this.ipcServer) {\n const server = this.ipcServer;\n this.ipcServer = null;\n await new Promise<void>((resolve) => server.close(() => resolve()));\n }\n if (this.endpoint.kind === 'unix') {\n try {\n fs.unlinkSync(this.endpoint.path);\n } catch {\n // noop\n }\n }\n this.removeAllListeners();\n }\n\n async handleRequest(request: SessionHostRequest): Promise<SessionHostResponse> {\n try {\n switch (request.type) {\n case 'create_session': {\n const record = this.registry.createSession(request.payload);\n this.schedulePersist(record.sessionId);\n this.emitEvent({ type: 'session_created', sessionId: record.sessionId, record });\n try {\n const startedRecord = this.startRuntime(record, request.payload, 'session_started');\n return { success: true, result: startedRecord };\n } catch (error: any) {\n this.registry.markStopped(record.sessionId, 'failed');\n this.persistNow(record.sessionId);\n return { success: false, error: error?.message || String(error) };\n }\n }\n case 'list_sessions':\n return { success: true, result: this.registry.listSessions() };\n case 'attach_session': {\n const record = this.registry.attachClient(request.payload);\n this.schedulePersist(record.sessionId);\n const client = record.attachedClients.find(item => item.clientId === request.payload.clientId);\n if (client) {\n this.emitEvent({ type: 'client_attached', sessionId: record.sessionId, client });\n }\n return { success: true, result: record };\n }\n case 'detach_session': {\n const record = this.registry.detachClient(request.payload);\n this.schedulePersist(record.sessionId);\n this.emitEvent({ type: 'client_detached', sessionId: record.sessionId, clientId: request.payload.clientId });\n return { success: true, result: record };\n }\n case 'acquire_write': {\n const record = this.registry.acquireWrite(request.payload);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'write_owner_changed', sessionId: record.sessionId, owner: record.writeOwner });\n return { success: true, result: record };\n }\n case 'release_write': {\n const record = this.registry.releaseWrite(request.payload);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'write_owner_changed', sessionId: record.sessionId, owner: record.writeOwner });\n return { success: true, result: record };\n }\n case 'get_snapshot':\n return { success: true, result: this.getSnapshot(request.payload.sessionId, request.payload.sinceSeq) };\n case 'clear_session_buffer': {\n const record = this.registry.clearBuffer(request.payload.sessionId);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'session_cleared', sessionId: record.sessionId });\n return { success: true, result: record };\n }\n case 'update_session_meta': {\n const record = this.registry.updateSessionMeta(\n request.payload.sessionId,\n request.payload.meta || {},\n request.payload.replace === true,\n );\n this.persistNow(record.sessionId);\n return { success: true, result: record };\n }\n case 'send_input': {\n const client = this.getAttachedClient(request.payload.sessionId, request.payload.clientId);\n if (client?.readOnly) {\n return { success: false, error: `Client ${request.payload.clientId} is read-only` };\n }\n const session = this.registry.getSession(request.payload.sessionId);\n if (session?.writeOwner && session.writeOwner.clientId !== request.payload.clientId) {\n return { success: false, error: `Write owned by ${session.writeOwner.clientId}` };\n }\n this.requireRuntime(request.payload.sessionId).write(request.payload.data);\n return { success: true, result: this.registry.getSession(request.payload.sessionId) };\n }\n case 'resize_session': {\n this.requireRuntime(request.payload.sessionId).resize(request.payload.cols, request.payload.rows);\n const record = this.registry.getSession(request.payload.sessionId);\n if (record) {\n this.registry.restoreSession(\n {\n ...record,\n meta: {\n ...(record.meta || {}),\n sessionHostCols: request.payload.cols,\n sessionHostRows: request.payload.rows,\n },\n },\n this.registry.getSnapshot(request.payload.sessionId),\n );\n }\n this.schedulePersist(request.payload.sessionId);\n this.emitEvent({\n type: 'session_resized',\n sessionId: request.payload.sessionId,\n cols: request.payload.cols,\n rows: request.payload.rows,\n });\n return { success: true, result: this.registry.getSession(request.payload.sessionId) };\n }\n case 'stop_session': {\n this.registry.setLifecycle(request.payload.sessionId, 'stopping');\n this.persistNow(request.payload.sessionId);\n this.requireRuntime(request.payload.sessionId).stop();\n this.emitEvent({ type: 'session_stopped', sessionId: request.payload.sessionId });\n return { success: true, result: this.registry.getSession(request.payload.sessionId) };\n }\n case 'resume_session': {\n const existing = this.registry.getSession(request.payload.sessionId);\n if (!existing) {\n return { success: false, error: `Unknown session: ${request.payload.sessionId}` };\n }\n if (this.runtimes.has(request.payload.sessionId)) {\n return { success: true, result: existing };\n }\n const resumed = this.startRuntime(existing, this.buildPayloadFromRecord(existing), 'session_resumed');\n return { success: true, result: resumed };\n }\n default:\n return { success: false, error: `Unsupported session host request: ${(request as { type?: string })?.type || 'unknown'}` };\n }\n } catch (error: any) {\n return { success: false, error: error?.message || String(error) };\n }\n }\n\n private requireRuntime(sessionId: string): PtySessionRuntime {\n const runtime = this.runtimes.get(sessionId);\n if (!runtime) throw new Error(`Runtime not found for session: ${sessionId}`);\n return runtime;\n }\n\n private getAttachedClient(sessionId: string, clientId: string): SessionAttachedClient | null {\n const session = this.registry.getSession(sessionId);\n return session?.attachedClients.find((client) => client.clientId === clientId) || null;\n }\n\n private emitEvent(event: SessionHostEvent): void {\n for (const socket of this.sockets) {\n writeEnvelope(socket, {\n kind: 'event',\n event,\n });\n }\n this.emit('event', event);\n }\n\n private async handleIncomingRequest(socket: net.Socket, envelope: SessionHostRequestEnvelope): Promise<void> {\n const response = await this.handleRequest(envelope.request);\n writeEnvelope(socket, createResponseEnvelope(envelope.requestId, response));\n }\n\n private schedulePersist(sessionId: string): void {\n const existing = this.persistTimers.get(sessionId);\n if (existing) clearTimeout(existing);\n this.persistTimers.set(sessionId, setTimeout(() => {\n this.persistTimers.delete(sessionId);\n this.persistNow(sessionId);\n }, 200));\n }\n\n private persistNow(sessionId: string): void {\n const record = this.registry.getSession(sessionId);\n if (!record) return;\n const snapshot = this.getSnapshot(sessionId);\n this.storage.save(record, snapshot);\n }\n\n private getSnapshot(sessionId: string, sinceSeq?: number) {\n const snapshot = this.registry.getSnapshot(sessionId, sinceSeq);\n const record = this.registry.getSession(sessionId);\n if (typeof sinceSeq === 'number') {\n return {\n ...snapshot,\n cols: typeof record?.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record?.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n };\n }\n\n const runtime = this.runtimes.get(sessionId);\n const runtimeText = runtime?.getSnapshotText?.() || '';\n if (!runtimeText) {\n return {\n ...snapshot,\n cols: typeof record?.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record?.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n };\n }\n\n return {\n ...snapshot,\n text: runtimeText,\n truncated: false,\n cols: typeof record?.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record?.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n };\n }\n\n flushAllPersistence(): void {\n for (const sessionId of this.runtimes.keys()) {\n this.persistNow(sessionId);\n }\n for (const record of this.registry.listSessions()) {\n this.persistNow(record.sessionId);\n }\n }\n\n private restorePersistedRuntimes(): void {\n const states = this.storage.loadAll();\n const runtimesToResume: Array<{\n persisted: PersistedRuntimeState;\n recoveredRecord: SessionHostRecord;\n }> = [];\n let skippedOrphanLiveSessions = 0;\n for (const persisted of states) {\n const wasLiveRuntime = !['stopped', 'failed'].includes(persisted.record.lifecycle);\n const hadAttachedClients = Array.isArray(persisted.record.attachedClients) && persisted.record.attachedClients.length > 0;\n const hadWriteOwner = !!persisted.record.writeOwner;\n const shouldAutoResume = wasLiveRuntime && (hadAttachedClients || hadWriteOwner);\n const recoveredRecord: SessionHostRecord = {\n ...persisted.record,\n attachedClients: [],\n writeOwner: null,\n lifecycle: shouldAutoResume ? 'interrupted' : (wasLiveRuntime ? 'stopped' : persisted.record.lifecycle),\n lastActivityAt: Date.now(),\n meta: {\n ...(persisted.record.meta || {}),\n restoredFromStorage: true,\n runtimeRecoveryState: shouldAutoResume\n ? 'host_restart_interrupted'\n : (wasLiveRuntime ? 'orphan_snapshot' : 'snapshot'),\n runtimeHadAttachedClientsAtCrash: hadAttachedClients,\n runtimeHadWriteOwnerAtCrash: hadWriteOwner,\n },\n };\n this.registry.restoreSession(recoveredRecord, persisted.snapshot);\n this.storage.save(recoveredRecord, persisted.snapshot);\n if (shouldAutoResume) {\n runtimesToResume.push({ persisted, recoveredRecord });\n } else if (wasLiveRuntime) {\n skippedOrphanLiveSessions += 1;\n }\n }\n\n if (skippedOrphanLiveSessions > 0) {\n this.emit('log', `session host skipped ${skippedOrphanLiveSessions} orphan live runtime(s) during restore`);\n }\n\n for (const { persisted, recoveredRecord } of runtimesToResume) {\n try {\n const resumed = this.startRuntime(\n recoveredRecord,\n this.buildPayloadFromRecord(recoveredRecord),\n 'session_resumed',\n );\n const resumedMeta = {\n ...(resumed.meta || {}),\n restoredFromStorage: true,\n runtimeRecoveryState: 'auto_resumed',\n };\n this.registry.restoreSession(\n { ...resumed, meta: resumedMeta },\n this.registry.getSnapshot(resumed.sessionId),\n );\n this.persistNow(resumed.sessionId);\n } catch (error: any) {\n const interrupted = this.registry.setLifecycle(recoveredRecord.sessionId, 'interrupted');\n this.registry.restoreSession({\n ...interrupted,\n meta: {\n ...(interrupted.meta || {}),\n restoredFromStorage: true,\n runtimeRecoveryState: 'resume_failed',\n runtimeRecoveryError: error?.message || String(error),\n },\n }, persisted.snapshot);\n this.persistNow(recoveredRecord.sessionId);\n }\n }\n }\n\n private buildPayloadFromRecord(record: SessionHostRecord): CreateSessionPayload {\n return {\n sessionId: record.sessionId,\n runtimeKey: record.runtimeKey,\n displayName: record.displayName,\n providerType: record.providerType,\n category: record.category,\n workspace: record.workspace,\n launchCommand: record.launchCommand,\n cols: typeof record.meta?.sessionHostCols === 'number' ? (record.meta.sessionHostCols as number) : 80,\n rows: typeof record.meta?.sessionHostRows === 'number' ? (record.meta.sessionHostRows as number) : 24,\n meta: record.meta,\n };\n }\n\n private startRuntime(\n record: SessionHostRecord,\n payload: CreateSessionPayload,\n startEventType: 'session_started' | 'session_resumed',\n ): SessionHostRecord {\n const runtime = new PtySessionRuntime({\n sessionId: record.sessionId,\n payload,\n onData: (data) => {\n const { seq } = this.registry.appendOutput(record.sessionId, data);\n this.schedulePersist(record.sessionId);\n this.emitEvent({ type: 'session_output', sessionId: record.sessionId, seq, data });\n },\n onExit: (exitCode) => {\n this.registry.markStopped(record.sessionId, exitCode === 0 ? 'stopped' : 'failed');\n this.runtimes.delete(record.sessionId);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: 'session_exit', sessionId: record.sessionId, exitCode });\n // Clean up persistence file after a brief delay (allow post-mortem reads)\n setTimeout(() => this.storage.remove(record.sessionId), 5_000);\n },\n });\n\n this.registry.setLifecycle(record.sessionId, 'starting');\n const pid = runtime.start();\n this.runtimes.set(record.sessionId, runtime);\n const startedRecord = this.registry.markStarted(record.sessionId, pid);\n this.persistNow(record.sessionId);\n this.emitEvent({ type: startEventType, sessionId: record.sessionId, pid });\n return startedRecord;\n }\n}\n","import * as fs from 'fs';\nimport * as os from 'os';\nimport * as path from 'path';\nimport * as pty from 'node-pty';\nimport type { IPty } from 'node-pty';\nimport type { CreateSessionPayload } from '@adhdev/session-host-core';\nimport { sanitizeSpawnEnv, ensureNodePtySpawnHelperPermissions } from '@adhdev/session-host-core';\n\ntype TerminalMirrorHandle = {\n write(data: string | Uint8Array): void;\n resize(cols: number, rows: number): void;\n formatVT(): string;\n getCursorPosition(): { col: number; row: number };\n dispose(): void;\n};\n\ntype GhosttyTerminalHandle = {\n write(data: string | Uint8Array): void;\n resize(cols: number, rows: number): void;\n formatVT(): string;\n getCursorPosition(): { col: number; row: number };\n dispose(): void;\n};\n\ntype GhosttyBinding = {\n createTerminal(options: { cols: number; rows: number; scrollback: number }): GhosttyTerminalHandle;\n};\n\ntype XtermBufferLine = {\n translateToString(trimRight?: boolean): string;\n};\n\ntype XtermBuffer = {\n length: number;\n viewportY: number;\n cursorX?: number;\n cursorY?: number;\n getLine(index: number): XtermBufferLine | undefined;\n};\n\ntype XtermTerminal = {\n buffer: { active: XtermBuffer };\n write(data: string, callback?: () => void): void;\n resize(cols: number, rows: number): void;\n dispose(): void;\n};\n\ntype XtermCtor = new (options: { cols: number; rows: number; scrollback: number }) => XtermTerminal;\n\nlet terminalMirrorFactory:\n | ((options: { cols: number; rows: number; scrollback: number }) => TerminalMirrorHandle)\n | null\n | undefined;\nlet terminalMirrorWarning: string | null = null;\nlet terminalMirrorBackendLogged = false;\n\nensureNodePtySpawnHelperPermissions((msg: string) => console.log(`[session-host] ${msg}`));\n\nfunction logTerminalMirrorBackend(message: string, level: 'info' | 'warn' = 'info'): void {\n if (terminalMirrorBackendLogged) return;\n terminalMirrorBackendLogged = true;\n const prefix = '[session-host]';\n if (level === 'warn') console.warn(`${prefix} ${message}`);\n else console.log(`${prefix} ${message}`);\n}\n\nexport interface PtyRuntimeOptions {\n sessionId: string;\n payload: CreateSessionPayload;\n onData: (data: string) => void;\n onExit: (exitCode: number | null) => void;\n}\n\n// Use shared spawn env sanitizer — alias for backward compat within this file\nconst buildRuntimeEnv = sanitizeSpawnEnv;\n\nfunction computeTerminalQueryTail(buffer: string): string {\n const prefixes = ['\\x1b[6n', '\\x1b[?6n'];\n const maxLength = prefixes.reduce((n, value) => Math.max(n, value.length), 0) - 1;\n const start = Math.max(0, buffer.length - maxLength);\n for (let i = start; i < buffer.length; i++) {\n const suffix = buffer.slice(i);\n if (prefixes.some((pattern) => suffix.length < pattern.length && pattern.startsWith(suffix))) {\n return suffix;\n }\n }\n return '';\n}\n\nfunction formatXtermViewport(terminal: XtermTerminal, rows: number): string {\n const buffer = terminal.buffer.active;\n const start = Math.max(0, buffer.viewportY || 0);\n const end = Math.max(start, Math.min(buffer.length || 0, start + Math.max(1, rows | 0)));\n const lines: string[] = [];\n\n for (let i = start; i < end; i++) {\n const line = buffer.getLine(i);\n lines.push(line ? line.translateToString(true) : '');\n }\n\n let first = 0;\n let last = lines.length;\n while (first < last && !lines[first]?.trim()) first++;\n while (last > first && !lines[last - 1]?.trim()) last--;\n return lines.slice(first, last).join('\\n');\n}\n\nfunction createXtermMirror(options: { cols: number; rows: number; scrollback: number }): TerminalMirrorHandle {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const mod = require('@xterm/xterm');\n const Terminal = (mod.Terminal || mod.default?.Terminal || mod.default) as XtermCtor | undefined;\n if (!Terminal) {\n throw new Error('@xterm/xterm Terminal export not found');\n }\n\n let currentRows = Math.max(1, options.rows | 0);\n const terminal = new Terminal({\n cols: Math.max(1, options.cols | 0),\n rows: currentRows,\n scrollback: Math.max(0, options.scrollback | 0),\n });\n\n return {\n write(data: string | Uint8Array): void {\n if (!data) return;\n terminal.write(typeof data === 'string' ? data : Buffer.from(data).toString('utf8'));\n },\n resize(cols: number, rows: number): void {\n currentRows = Math.max(1, rows | 0);\n terminal.resize(Math.max(1, cols | 0), currentRows);\n },\n formatVT(): string {\n return formatXtermViewport(terminal, currentRows);\n },\n getCursorPosition(): { col: number; row: number } {\n const buffer = terminal.buffer.active;\n return {\n col: Math.max(0, buffer.cursorX || 0),\n row: Math.max(0, buffer.cursorY || 0),\n };\n },\n dispose(): void {\n terminal.dispose();\n },\n };\n}\n\nfunction normalizeGhosttyBinding(mod: any): GhosttyBinding | null {\n const raw = mod?.default?.createTerminal ? mod.default : mod?.createTerminal ? mod : null;\n if (!raw) return null;\n\n // Wrap the native handle to fill in any missing methods (pre-built binaries may lack\n // formatVT / getCursorPosition if built before those methods were added to the Rust side).\n return {\n createTerminal(options: { cols: number; rows: number; scrollback: number }): GhosttyTerminalHandle {\n const handle = raw.createTerminal(options) as any;\n return {\n write(data: string | Uint8Array): void { handle.write(data); },\n resize(cols: number, rows: number): void { handle.resize(cols, rows); },\n formatVT(): string {\n if (typeof handle.formatVT === 'function') return handle.formatVT();\n // Fallback: formatPlainText is always available in the shipped bindings\n if (typeof handle.formatPlainText === 'function') return handle.formatPlainText({ trim: false }) as string;\n return '';\n },\n getCursorPosition(): { col: number; row: number } {\n if (typeof handle.getCursorPosition === 'function') return handle.getCursorPosition() as { col: number; row: number };\n return { col: 0, row: 0 };\n },\n dispose(): void { handle.dispose(); },\n };\n },\n };\n}\n\nfunction getTerminalMirrorFactory(): (options: { cols: number; rows: number; scrollback: number }) => TerminalMirrorHandle {\n if (terminalMirrorFactory) return terminalMirrorFactory;\n if (terminalMirrorFactory === null) {\n throw new Error(terminalMirrorWarning || 'No terminal mirror backend available');\n }\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const ghosttyMod = require('@adhdev/ghostty-vt-node');\n const binding = normalizeGhosttyBinding(ghosttyMod);\n if (!binding) {\n throw new Error('@adhdev/ghostty-vt-node does not export createTerminal()');\n }\n terminalMirrorFactory = (options) => binding.createTerminal(options);\n logTerminalMirrorBackend('terminal mirror backend=ghostty-vt');\n return terminalMirrorFactory;\n } catch (ghosttyError: any) {\n try {\n terminalMirrorFactory = createXtermMirror;\n terminalMirrorWarning = `Ghostty VT unavailable; falling back to xterm mirror (${ghosttyError?.message || String(ghosttyError)})`;\n logTerminalMirrorBackend(terminalMirrorWarning, 'warn');\n return terminalMirrorFactory;\n } catch (xtermError: any) {\n terminalMirrorFactory = null;\n terminalMirrorWarning = `No terminal mirror backend available (ghostty: ${ghosttyError?.message || String(ghosttyError)}; xterm: ${xtermError?.message || String(xtermError)})`;\n logTerminalMirrorBackend(terminalMirrorWarning, 'warn');\n throw new Error(terminalMirrorWarning);\n }\n }\n}\n\nexport class PtySessionRuntime {\n readonly sessionId: string;\n readonly payload: CreateSessionPayload;\n readonly cols: number;\n readonly rows: number;\n\n private ptyProcess: IPty | null = null;\n private screenMirror: TerminalMirrorHandle | null = null;\n private pendingQueryScanTail = '';\n private onDataCallback: (data: string) => void;\n private onExitCallback: (exitCode: number | null) => void;\n\n constructor(options: PtyRuntimeOptions) {\n this.sessionId = options.sessionId;\n this.payload = options.payload;\n this.cols = options.payload.cols || 80;\n this.rows = options.payload.rows || 24;\n this.onDataCallback = options.onData;\n this.onExitCallback = options.onExit;\n }\n\n start(): number {\n if (this.ptyProcess) return this.ptyProcess.pid;\n\n const command = this.payload.launchCommand.command;\n const args = this.payload.launchCommand.args || [];\n const env = buildRuntimeEnv(process.env, this.payload.launchCommand.env);\n\n // Validate workspace directory — an invalid cwd causes a native crash on Windows\n // (node-pty error code 267: ERROR_DIRECTORY) that bypasses JS try/catch\n let cwd = this.payload.workspace || process.cwd();\n if (cwd) {\n try {\n const stat = fs.statSync(cwd);\n if (!stat.isDirectory()) cwd = os.homedir();\n } catch {\n cwd = os.homedir();\n }\n }\n\n this.ptyProcess = pty.spawn(command, args, {\n name: 'xterm-256color',\n cols: this.cols,\n rows: this.rows,\n cwd,\n env,\n });\n this.screenMirror = getTerminalMirrorFactory()({\n cols: this.cols,\n rows: this.rows,\n scrollback: 32768,\n });\n\n this.ptyProcess.onData((data: string) => {\n this.screenMirror?.write(data);\n this.respondToTerminalQueries(data);\n this.onDataCallback(data);\n });\n\n this.ptyProcess.onExit(({ exitCode }) => {\n this.ptyProcess = null;\n this.screenMirror?.dispose();\n this.screenMirror = null;\n this.pendingQueryScanTail = '';\n this.onExitCallback(exitCode ?? null);\n });\n\n return this.ptyProcess.pid;\n }\n\n write(data: string): void {\n if (!this.ptyProcess) throw new Error(`Session not running: ${this.sessionId}`);\n this.ptyProcess.write(data);\n }\n\n resize(cols: number, rows: number): void {\n if (!this.ptyProcess) throw new Error(`Session not running: ${this.sessionId}`);\n this.ptyProcess.resize(cols, rows);\n this.screenMirror?.resize(cols, rows);\n }\n\n stop(): void {\n if (!this.ptyProcess) return;\n this.ptyProcess.kill();\n }\n\n getSnapshotText(): string {\n return this.screenMirror?.formatVT() || '';\n }\n\n private respondToTerminalQueries(data: string): void {\n if (!this.ptyProcess || !this.screenMirror || !data) return;\n\n const combined = this.pendingQueryScanTail + data;\n const regex = /\\x1b\\[(\\?)?6n/g;\n let match: RegExpExecArray | null;\n\n while ((match = regex.exec(combined)) !== null) {\n const cursor = this.screenMirror.getCursorPosition();\n const row = Math.max(1, (cursor.row | 0) + 1);\n const col = Math.max(1, (cursor.col | 0) + 1);\n const response = match[1]\n ? `\\x1b[?${row};${col}R`\n : `\\x1b[${row};${col}R`;\n this.ptyProcess.write(response);\n }\n\n this.pendingQueryScanTail = computeTerminalQueryTail(combined);\n }\n}\n","import * as fs from 'fs';\nimport * as os from 'os';\nimport * as path from 'path';\nimport type { SessionBufferSnapshot, SessionHostRecord } from '@adhdev/session-host-core';\n\nexport interface PersistedRuntimeState {\n record: SessionHostRecord;\n snapshot: SessionBufferSnapshot;\n updatedAt: number;\n}\n\ninterface SessionHostStorageOptions {\n appName?: string;\n}\n\nexport class SessionHostStorage {\n private readonly rootDir: string;\n private readonly runtimesDir: string;\n\n constructor(options: SessionHostStorageOptions = {}) {\n const appName = options.appName || 'adhdev';\n this.rootDir = path.join(os.homedir(), '.adhdev', 'session-host', appName);\n this.runtimesDir = path.join(this.rootDir, 'runtimes');\n }\n\n loadAll(): PersistedRuntimeState[] {\n if (!fs.existsSync(this.runtimesDir)) return [];\n const entries = fs.readdirSync(this.runtimesDir, { withFileTypes: true });\n const states: PersistedRuntimeState[] = [];\n for (const entry of entries) {\n if (!entry.isFile() || !entry.name.endsWith('.json')) continue;\n const fullPath = path.join(this.runtimesDir, entry.name);\n try {\n const parsed = JSON.parse(fs.readFileSync(fullPath, 'utf8')) as PersistedRuntimeState;\n if (parsed?.record?.sessionId) {\n states.push(parsed);\n }\n } catch {\n // Ignore malformed snapshots; host should still boot.\n }\n }\n return states.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));\n }\n\n save(record: SessionHostRecord, snapshot: SessionBufferSnapshot): void {\n fs.mkdirSync(this.runtimesDir, { recursive: true });\n const filePath = path.join(this.runtimesDir, `${record.sessionId}.json`);\n const payload: PersistedRuntimeState = {\n record,\n snapshot,\n updatedAt: Date.now(),\n };\n fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), 'utf8');\n }\n\n remove(sessionId: string): void {\n const filePath = path.join(this.runtimesDir, `${sessionId}.json`);\n try {\n fs.unlinkSync(filePath);\n } catch {\n // File may not exist — ignore.\n }\n }\n}\n"],"mappings":";;;;;;;;;AAEA,SAAS,kBAAkB;AAC3B,YAAYA,SAAQ;AACpB,YAAYC,SAAQ;AACpB,YAAYC,WAAU;AACtB;AAAA,EACE;AAAA,EACA;AAAA,EACA,iCAAAC;AAAA,EACA;AAAA,OAGK;;;ACbP,SAAS,oBAAoB;AAC7B,YAAYC,SAAQ;AACpB,YAAY,SAAS;AACrB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACTP,YAAY,QAAQ;AACpB,YAAY,QAAQ;AAEpB,YAAY,SAAS;AAGrB,SAAS,kBAAkB,2CAA2C;AA2Ct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,MACF,CAAC;AACD,UAAI,CAAC,eAAe,WAAW,CAAC,eAAe,QAAQ;AACrD,cAAM,IAAI,MAAM,eAAe,SAAS,4BAA4B,cAAc,UAAU,EAAE;AAAA,MAChG;AACA,cAAQ,IAAI,WAAW,eAAe,OAAO,UAAU,KAAK,eAAe,OAAO,SAAS,GAAG;AAAA,IAChG,UAAE;AACA,YAAM,OAAO,MAAM,EAAE,MAAM,MAAM;AAAA,MAAC,CAAC;AAAA,IACrC;AACA;AAAA,EACF;AACA,QAAM,IAAI,MAAM,oBAAoB,OAAO,EAAE;AAC/C;AAEA,IAAI,UAAQ,SAAS,QAAQ;AAE3B,UAAQ,GAAG,qBAAqB,CAAC,QAAQ;AACvC,YAAQ,MAAM,sCAAsC,KAAK,OAAO;AAAA,EAAK,KAAK,SAAS,EAAE,EAAE;AAAA,EAEzF,CAAC;AACD,UAAQ,GAAG,sBAAsB,CAAC,WAAgB;AAChD,YAAQ,MAAM,uCAAuC,QAAQ,WAAW,MAAM,EAAE;AAAA,EAClF,CAAC;AAED,OAAK,KAAK,EAAE,MAAM,CAAC,UAAU;AAC3B,YAAQ,MAAM,iBAAiB,QAAQ,MAAM,UAAU,KAAK;AAC5D,YAAQ,KAAK,CAAC;AAAA,EAChB,CAAC;AACH;","names":["fs","os","path","getDefaultSessionHostEndpoint","fs","fs","os","getDefaultSessionHostEndpoint"]}
@@ -301,4 +301,33 @@ declare class SessionHostClient {
301
301
  declare function createResponseEnvelope(requestId: string, response: SessionHostResponse): SessionHostResponseEnvelope;
302
302
  declare function writeEnvelope(socket: Pick<net.Socket, 'write'>, envelope: SessionHostWireEnvelope): void;
303
303
 
304
- export { type AcquireWritePayload, type AttachSessionPayload, type ClearSessionBufferPayload, type CreateSessionPayload, type DetachSessionPayload, type GetSnapshotPayload, type ReleaseWritePayload, type ResizeSessionPayload, type ResumeSessionPayload, type SendInputPayload, type SessionAttachedClient, type SessionBufferSnapshot, type SessionClientType, type SessionHostCategory, SessionHostClient, type SessionHostClientOptions, type SessionHostEndpoint, type SessionHostEvent, type SessionHostEventEnvelope, type SessionHostRecord, SessionHostRegistry, type SessionHostRequest, type SessionHostRequestEnvelope, type SessionHostResponse, type SessionHostResponseEnvelope, type SessionHostWireEnvelope, type SessionLaunchCommand, type SessionLifecycle, type SessionOwnerType, SessionRingBuffer, type SessionRingBufferOptions, type SessionTransport, type SessionWriteOwner, type StopSessionPayload, type UpdateSessionMetaPayload, buildRuntimeDisplayName, buildRuntimeKey, createLineParser, createResponseEnvelope, formatRuntimeOwner, getDefaultSessionHostEndpoint, getWorkspaceLabel, resolveRuntimeRecord, writeEnvelope };
304
+ /**
305
+ * Shared PTY spawn environment utilities.
306
+ *
307
+ * Centralises npm/pnpm/yarn env variable stripping, terminal colour env
308
+ * injection, and node-pty spawn-helper permission fixing.
309
+ *
310
+ * Used by daemon-core (provider-cli-adapter), session-host-daemon (runtime),
311
+ * and daemon-cloud (session-host).
312
+ */
313
+ /**
314
+ * Strip package-manager injected environment variables that can interfere
315
+ * with child CLI processes and apply terminal colour defaults.
316
+ */
317
+ declare function sanitizeSpawnEnv(baseEnv: NodeJS.ProcessEnv, overrides?: Record<string, string>): Record<string, string>;
318
+ /**
319
+ * Apply preferred terminal colour environment variables.
320
+ * Ensures TERM is set to xterm-256color and enables colour on Windows.
321
+ */
322
+ declare function applyTerminalColorEnv(env: Record<string, string>): void;
323
+ /**
324
+ * Ensure node-pty's spawn-helper binary has execute permissions.
325
+ *
326
+ * npm's default umask can strip +x from the prebuilt spawn-helper on macOS/Linux,
327
+ * causing EACCES when node-pty tries to fork. Best-effort fix.
328
+ *
329
+ * @param logFn Optional log callback for reporting the fix.
330
+ */
331
+ declare function ensureNodePtySpawnHelperPermissions(logFn?: (msg: string) => void): void;
332
+
333
+ export { type AcquireWritePayload, type AttachSessionPayload, type ClearSessionBufferPayload, type CreateSessionPayload, type DetachSessionPayload, type GetSnapshotPayload, type ReleaseWritePayload, type ResizeSessionPayload, type ResumeSessionPayload, type SendInputPayload, type SessionAttachedClient, type SessionBufferSnapshot, type SessionClientType, type SessionHostCategory, SessionHostClient, type SessionHostClientOptions, type SessionHostEndpoint, type SessionHostEvent, type SessionHostEventEnvelope, type SessionHostRecord, SessionHostRegistry, type SessionHostRequest, type SessionHostRequestEnvelope, type SessionHostResponse, type SessionHostResponseEnvelope, type SessionHostWireEnvelope, type SessionLaunchCommand, type SessionLifecycle, type SessionOwnerType, SessionRingBuffer, type SessionRingBufferOptions, type SessionTransport, type SessionWriteOwner, type StopSessionPayload, type UpdateSessionMetaPayload, applyTerminalColorEnv, buildRuntimeDisplayName, buildRuntimeKey, createLineParser, createResponseEnvelope, ensureNodePtySpawnHelperPermissions, formatRuntimeOwner, getDefaultSessionHostEndpoint, getWorkspaceLabel, resolveRuntimeRecord, sanitizeSpawnEnv, writeEnvelope };
@@ -301,4 +301,33 @@ declare class SessionHostClient {
301
301
  declare function createResponseEnvelope(requestId: string, response: SessionHostResponse): SessionHostResponseEnvelope;
302
302
  declare function writeEnvelope(socket: Pick<net.Socket, 'write'>, envelope: SessionHostWireEnvelope): void;
303
303
 
304
- export { type AcquireWritePayload, type AttachSessionPayload, type ClearSessionBufferPayload, type CreateSessionPayload, type DetachSessionPayload, type GetSnapshotPayload, type ReleaseWritePayload, type ResizeSessionPayload, type ResumeSessionPayload, type SendInputPayload, type SessionAttachedClient, type SessionBufferSnapshot, type SessionClientType, type SessionHostCategory, SessionHostClient, type SessionHostClientOptions, type SessionHostEndpoint, type SessionHostEvent, type SessionHostEventEnvelope, type SessionHostRecord, SessionHostRegistry, type SessionHostRequest, type SessionHostRequestEnvelope, type SessionHostResponse, type SessionHostResponseEnvelope, type SessionHostWireEnvelope, type SessionLaunchCommand, type SessionLifecycle, type SessionOwnerType, SessionRingBuffer, type SessionRingBufferOptions, type SessionTransport, type SessionWriteOwner, type StopSessionPayload, type UpdateSessionMetaPayload, buildRuntimeDisplayName, buildRuntimeKey, createLineParser, createResponseEnvelope, formatRuntimeOwner, getDefaultSessionHostEndpoint, getWorkspaceLabel, resolveRuntimeRecord, writeEnvelope };
304
+ /**
305
+ * Shared PTY spawn environment utilities.
306
+ *
307
+ * Centralises npm/pnpm/yarn env variable stripping, terminal colour env
308
+ * injection, and node-pty spawn-helper permission fixing.
309
+ *
310
+ * Used by daemon-core (provider-cli-adapter), session-host-daemon (runtime),
311
+ * and daemon-cloud (session-host).
312
+ */
313
+ /**
314
+ * Strip package-manager injected environment variables that can interfere
315
+ * with child CLI processes and apply terminal colour defaults.
316
+ */
317
+ declare function sanitizeSpawnEnv(baseEnv: NodeJS.ProcessEnv, overrides?: Record<string, string>): Record<string, string>;
318
+ /**
319
+ * Apply preferred terminal colour environment variables.
320
+ * Ensures TERM is set to xterm-256color and enables colour on Windows.
321
+ */
322
+ declare function applyTerminalColorEnv(env: Record<string, string>): void;
323
+ /**
324
+ * Ensure node-pty's spawn-helper binary has execute permissions.
325
+ *
326
+ * npm's default umask can strip +x from the prebuilt spawn-helper on macOS/Linux,
327
+ * causing EACCES when node-pty tries to fork. Best-effort fix.
328
+ *
329
+ * @param logFn Optional log callback for reporting the fix.
330
+ */
331
+ declare function ensureNodePtySpawnHelperPermissions(logFn?: (msg: string) => void): void;
332
+
333
+ export { type AcquireWritePayload, type AttachSessionPayload, type ClearSessionBufferPayload, type CreateSessionPayload, type DetachSessionPayload, type GetSnapshotPayload, type ReleaseWritePayload, type ResizeSessionPayload, type ResumeSessionPayload, type SendInputPayload, type SessionAttachedClient, type SessionBufferSnapshot, type SessionClientType, type SessionHostCategory, SessionHostClient, type SessionHostClientOptions, type SessionHostEndpoint, type SessionHostEvent, type SessionHostEventEnvelope, type SessionHostRecord, SessionHostRegistry, type SessionHostRequest, type SessionHostRequestEnvelope, type SessionHostResponse, type SessionHostResponseEnvelope, type SessionHostWireEnvelope, type SessionLaunchCommand, type SessionLifecycle, type SessionOwnerType, SessionRingBuffer, type SessionRingBufferOptions, type SessionTransport, type SessionWriteOwner, type StopSessionPayload, type UpdateSessionMetaPayload, applyTerminalColorEnv, buildRuntimeDisplayName, buildRuntimeKey, createLineParser, createResponseEnvelope, ensureNodePtySpawnHelperPermissions, formatRuntimeOwner, getDefaultSessionHostEndpoint, getWorkspaceLabel, resolveRuntimeRecord, sanitizeSpawnEnv, writeEnvelope };