pty-manager 1.9.7 → 1.9.8

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.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,30 @@
1
1
  import { EventEmitter } from 'events';
2
2
 
3
+ /**
4
+ * Lazy runtime check for node-pty native addon.
5
+ *
6
+ * Called once before the first PTY spawn. Ensures the native binary is
7
+ * loadable and spawn-helper permissions are correct.
8
+ *
9
+ * 1. Finds the binary — checks for prebuilt pty.node under
10
+ * prebuilds/<platform>-<arch>/ (node-pty >=1.0), then falls back to
11
+ * checking for a node-gyp compiled build/Release/pty.node
12
+ * 2. Fixes spawn-helper permissions — bun install can strip execute
13
+ * bits from the spawn-helper Mach-O binary, causing posix_spawnp
14
+ * failed at runtime. The script chmod 755s all spawn-helpers under
15
+ * prebuilds/
16
+ * 3. Rebuilds if missing — if no binary is found at all, runs
17
+ * node-gyp rebuild as a last resort (with a 2-minute timeout)
18
+ */
19
+ /**
20
+ * Ensure node-pty is usable. Called once before first spawn.
21
+ * Idempotent — subsequent calls are no-ops.
22
+ *
23
+ * @param log - logger function (defaults to console.log)
24
+ * @throws Error if no native binary can be found or built
25
+ */
26
+ declare function ensurePty(log?: (msg: string) => void): void;
27
+
3
28
  /**
4
29
  * PTY Agent Manager Types
5
30
  *
@@ -1251,4 +1276,4 @@ declare function isBun(): boolean;
1251
1276
  */
1252
1277
  declare function createPTYManager(options?: BunPTYManagerOptions): BunCompatiblePTYManager;
1253
1278
 
1254
- export { type AdapterFactoryConfig, AdapterRegistry, type AuthRequiredInfo, type AuthRequiredMethod, type AutoResponseRule, BaseCLIAdapter, type BlockingPromptDetection, type BlockingPromptInfo, type BlockingPromptType, type BuildTimelineOptions, BunCompatiblePTYManager, type BunPTYManagerOptions, type CLIAdapter, type LogOptions, type Logger, type LoginDetection, type MessageType, PTYManager, type PTYManagerConfig, type PTYManagerEvents, PTYSession, type PTYSessionEvents, type ParsedOutput, SPECIAL_KEYS, type SessionFilter, type SessionHandle, type SessionMessage, type SessionStatus, ShellAdapter, type ShellAdapterOptions, type SpawnConfig, type StallClassification, type StopOptions, type TaskCompletionTimelineResult, type TaskCompletionTimelineStep, type TaskCompletionTraceRecord, type TaskCompletionTurnTimeline, type TerminalAttachment, type ToolRunningInfo, type WorkerSessionHandle, buildTaskCompletionTimeline, createAdapter, createPTYManager, extractTaskCompletionTraceRecords, isBun };
1279
+ export { type AdapterFactoryConfig, AdapterRegistry, type AuthRequiredInfo, type AuthRequiredMethod, type AutoResponseRule, BaseCLIAdapter, type BlockingPromptDetection, type BlockingPromptInfo, type BlockingPromptType, type BuildTimelineOptions, BunCompatiblePTYManager, type BunPTYManagerOptions, type CLIAdapter, type LogOptions, type Logger, type LoginDetection, type MessageType, PTYManager, type PTYManagerConfig, type PTYManagerEvents, PTYSession, type PTYSessionEvents, type ParsedOutput, SPECIAL_KEYS, type SessionFilter, type SessionHandle, type SessionMessage, type SessionStatus, ShellAdapter, type ShellAdapterOptions, type SpawnConfig, type StallClassification, type StopOptions, type TaskCompletionTimelineResult, type TaskCompletionTimelineStep, type TaskCompletionTraceRecord, type TaskCompletionTurnTimeline, type TerminalAttachment, type ToolRunningInfo, type WorkerSessionHandle, buildTaskCompletionTimeline, createAdapter, createPTYManager, ensurePty, extractTaskCompletionTraceRecords, isBun };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,30 @@
1
1
  import { EventEmitter } from 'events';
2
2
 
3
+ /**
4
+ * Lazy runtime check for node-pty native addon.
5
+ *
6
+ * Called once before the first PTY spawn. Ensures the native binary is
7
+ * loadable and spawn-helper permissions are correct.
8
+ *
9
+ * 1. Finds the binary — checks for prebuilt pty.node under
10
+ * prebuilds/<platform>-<arch>/ (node-pty >=1.0), then falls back to
11
+ * checking for a node-gyp compiled build/Release/pty.node
12
+ * 2. Fixes spawn-helper permissions — bun install can strip execute
13
+ * bits from the spawn-helper Mach-O binary, causing posix_spawnp
14
+ * failed at runtime. The script chmod 755s all spawn-helpers under
15
+ * prebuilds/
16
+ * 3. Rebuilds if missing — if no binary is found at all, runs
17
+ * node-gyp rebuild as a last resort (with a 2-minute timeout)
18
+ */
19
+ /**
20
+ * Ensure node-pty is usable. Called once before first spawn.
21
+ * Idempotent — subsequent calls are no-ops.
22
+ *
23
+ * @param log - logger function (defaults to console.log)
24
+ * @throws Error if no native binary can be found or built
25
+ */
26
+ declare function ensurePty(log?: (msg: string) => void): void;
27
+
3
28
  /**
4
29
  * PTY Agent Manager Types
5
30
  *
@@ -1251,4 +1276,4 @@ declare function isBun(): boolean;
1251
1276
  */
1252
1277
  declare function createPTYManager(options?: BunPTYManagerOptions): BunCompatiblePTYManager;
1253
1278
 
1254
- export { type AdapterFactoryConfig, AdapterRegistry, type AuthRequiredInfo, type AuthRequiredMethod, type AutoResponseRule, BaseCLIAdapter, type BlockingPromptDetection, type BlockingPromptInfo, type BlockingPromptType, type BuildTimelineOptions, BunCompatiblePTYManager, type BunPTYManagerOptions, type CLIAdapter, type LogOptions, type Logger, type LoginDetection, type MessageType, PTYManager, type PTYManagerConfig, type PTYManagerEvents, PTYSession, type PTYSessionEvents, type ParsedOutput, SPECIAL_KEYS, type SessionFilter, type SessionHandle, type SessionMessage, type SessionStatus, ShellAdapter, type ShellAdapterOptions, type SpawnConfig, type StallClassification, type StopOptions, type TaskCompletionTimelineResult, type TaskCompletionTimelineStep, type TaskCompletionTraceRecord, type TaskCompletionTurnTimeline, type TerminalAttachment, type ToolRunningInfo, type WorkerSessionHandle, buildTaskCompletionTimeline, createAdapter, createPTYManager, extractTaskCompletionTraceRecords, isBun };
1279
+ export { type AdapterFactoryConfig, AdapterRegistry, type AuthRequiredInfo, type AuthRequiredMethod, type AutoResponseRule, BaseCLIAdapter, type BlockingPromptDetection, type BlockingPromptInfo, type BlockingPromptType, type BuildTimelineOptions, BunCompatiblePTYManager, type BunPTYManagerOptions, type CLIAdapter, type LogOptions, type Logger, type LoginDetection, type MessageType, PTYManager, type PTYManagerConfig, type PTYManagerEvents, PTYSession, type PTYSessionEvents, type ParsedOutput, SPECIAL_KEYS, type SessionFilter, type SessionHandle, type SessionMessage, type SessionStatus, ShellAdapter, type ShellAdapterOptions, type SpawnConfig, type StallClassification, type StopOptions, type TaskCompletionTimelineResult, type TaskCompletionTimelineStep, type TaskCompletionTraceRecord, type TaskCompletionTurnTimeline, type TerminalAttachment, type ToolRunningInfo, type WorkerSessionHandle, buildTaskCompletionTimeline, createAdapter, createPTYManager, ensurePty, extractTaskCompletionTraceRecords, isBun };
package/dist/index.js CHANGED
@@ -40,11 +40,123 @@ __export(index_exports, {
40
40
  buildTaskCompletionTimeline: () => buildTaskCompletionTimeline,
41
41
  createAdapter: () => createAdapter,
42
42
  createPTYManager: () => createPTYManager,
43
+ ensurePty: () => ensurePty,
43
44
  extractTaskCompletionTraceRecords: () => extractTaskCompletionTraceRecords,
44
45
  isBun: () => isBun
45
46
  });
46
47
  module.exports = __toCommonJS(index_exports);
47
48
 
49
+ // src/ensure-pty.ts
50
+ var import_fs = require("fs");
51
+ var import_path = require("path");
52
+ var import_child_process = require("child_process");
53
+ var TAG = "[pty-preflight]";
54
+ var platformArch = `${process.platform}-${process.arch}`;
55
+ var checked = false;
56
+ function findNodePtyRoots() {
57
+ const roots = [];
58
+ try {
59
+ const nodePtyMain = require.resolve("node-pty");
60
+ const nodePtyRoot = (0, import_path.dirname)((0, import_path.dirname)(nodePtyMain));
61
+ if ((0, import_fs.existsSync)((0, import_path.join)(nodePtyRoot, "package.json"))) {
62
+ roots.push(nodePtyRoot);
63
+ }
64
+ } catch {
65
+ }
66
+ const packageRoot = (0, import_path.join)(__dirname, "..");
67
+ const candidates = [
68
+ (0, import_path.join)(packageRoot, "node_modules", "node-pty"),
69
+ // Monorepo hoisted
70
+ (0, import_path.join)(packageRoot, "..", "node-pty"),
71
+ (0, import_path.join)(packageRoot, "..", "..", "node-pty")
72
+ ];
73
+ for (const candidate of candidates) {
74
+ if ((0, import_fs.existsSync)((0, import_path.join)(candidate, "package.json")) && !roots.includes(candidate)) {
75
+ roots.push(candidate);
76
+ }
77
+ }
78
+ return roots;
79
+ }
80
+ function findNativeBinary(nodePtyRoot) {
81
+ const prebuildPath = (0, import_path.join)(nodePtyRoot, "prebuilds", platformArch, "pty.node");
82
+ if ((0, import_fs.existsSync)(prebuildPath)) {
83
+ return { type: "prebuild", path: prebuildPath };
84
+ }
85
+ const gypPath = (0, import_path.join)(nodePtyRoot, "build", "Release", "pty.node");
86
+ if ((0, import_fs.existsSync)(gypPath)) {
87
+ return { type: "gyp", path: gypPath };
88
+ }
89
+ return null;
90
+ }
91
+ function fixSpawnHelpers(nodePtyRoot, log) {
92
+ if (process.platform === "win32") return;
93
+ const prebuildsDir = (0, import_path.join)(nodePtyRoot, "prebuilds");
94
+ if (!(0, import_fs.existsSync)(prebuildsDir)) return;
95
+ try {
96
+ for (const entry of (0, import_fs.readdirSync)(prebuildsDir)) {
97
+ const helperPath = (0, import_path.join)(prebuildsDir, entry, "spawn-helper");
98
+ if ((0, import_fs.existsSync)(helperPath)) {
99
+ try {
100
+ const stat = (0, import_fs.statSync)(helperPath);
101
+ if ((stat.mode & 73) === 0) {
102
+ (0, import_fs.chmodSync)(helperPath, 493);
103
+ log(`${TAG} Fixed spawn-helper permissions: ${(0, import_path.relative)(nodePtyRoot, helperPath)}`);
104
+ }
105
+ } catch {
106
+ }
107
+ }
108
+ }
109
+ } catch {
110
+ }
111
+ }
112
+ function rebuildNodePty(nodePtyRoot, log) {
113
+ log(`${TAG} No native binary found \u2014 attempting node-gyp rebuild...`);
114
+ try {
115
+ (0, import_child_process.execSync)("node-gyp rebuild", {
116
+ cwd: nodePtyRoot,
117
+ stdio: "pipe",
118
+ timeout: 12e4
119
+ });
120
+ log(`${TAG} node-gyp rebuild succeeded`);
121
+ return true;
122
+ } catch (err) {
123
+ log(`${TAG} node-gyp rebuild failed: ${err instanceof Error ? err.message : err}`);
124
+ return false;
125
+ }
126
+ }
127
+ function ensurePty(log = console.log) {
128
+ if (checked) return;
129
+ checked = true;
130
+ const roots = findNodePtyRoots();
131
+ if (roots.length === 0) {
132
+ throw new Error(
133
+ `${TAG} node-pty not found. Install it with: npm install node-pty`
134
+ );
135
+ }
136
+ let anyBinaryFound = false;
137
+ for (const root of roots) {
138
+ const binary = findNativeBinary(root);
139
+ if (binary) {
140
+ log(`${TAG} Found ${binary.type} binary for ${platformArch}`);
141
+ anyBinaryFound = true;
142
+ }
143
+ fixSpawnHelpers(root, log);
144
+ if (!binary) {
145
+ if (rebuildNodePty(root, log)) {
146
+ if (findNativeBinary(root)) {
147
+ anyBinaryFound = true;
148
+ }
149
+ }
150
+ }
151
+ if (anyBinaryFound) break;
152
+ }
153
+ if (!anyBinaryFound) {
154
+ throw new Error(
155
+ `${TAG} No node-pty native binary available for ${platformArch}. Try: cd node_modules/node-pty && node-gyp rebuild`
156
+ );
157
+ }
158
+ }
159
+
48
160
  // src/pty-manager.ts
49
161
  var import_events2 = require("events");
50
162
 
@@ -896,6 +1008,7 @@ var PTYSession = class _PTYSession extends import_events.EventEmitter {
896
1008
  if (this.ptyProcess) {
897
1009
  throw new Error("Session already started");
898
1010
  }
1011
+ ensurePty((msg) => this.logger.info({ sessionId: this.id }, msg));
899
1012
  const nodePty = loadPty();
900
1013
  this._status = "starting";
901
1014
  this._startedAt = /* @__PURE__ */ new Date();
@@ -2001,7 +2114,7 @@ function asTimestamp(value) {
2001
2114
  }
2002
2115
 
2003
2116
  // src/adapters/base-adapter.ts
2004
- var import_child_process = require("child_process");
2117
+ var import_child_process2 = require("child_process");
2005
2118
  var BaseCLIAdapter = class {
2006
2119
  /**
2007
2120
  * Auto-response rules for handling known blocking prompts.
@@ -2167,7 +2280,7 @@ var BaseCLIAdapter = class {
2167
2280
  return new Promise((resolve) => {
2168
2281
  const command = this.getCommand();
2169
2282
  try {
2170
- const proc = (0, import_child_process.spawn)(command, ["--version"], {
2283
+ const proc = (0, import_child_process2.spawn)(command, ["--version"], {
2171
2284
  shell: true,
2172
2285
  timeout: 5e3
2173
2286
  });
@@ -2430,7 +2543,7 @@ var ShellAdapter = class {
2430
2543
  };
2431
2544
 
2432
2545
  // src/bun-compat.ts
2433
- var import_child_process2 = require("child_process");
2546
+ var import_child_process3 = require("child_process");
2434
2547
  var import_events3 = require("events");
2435
2548
  var path = __toESM(require("path"));
2436
2549
  var readline = __toESM(require("readline"));
@@ -2471,7 +2584,7 @@ var BunCompatiblePTYManager = class extends import_events3.EventEmitter {
2471
2584
  return possiblePaths[0];
2472
2585
  }
2473
2586
  startWorker() {
2474
- this.worker = (0, import_child_process2.spawn)(this.nodePath, [this.workerPath], {
2587
+ this.worker = (0, import_child_process3.spawn)(this.nodePath, [this.workerPath], {
2475
2588
  stdio: ["pipe", "pipe", "pipe"],
2476
2589
  env: { ...process.env, ...this.env }
2477
2590
  });
@@ -2982,6 +3095,7 @@ function createPTYManager(options) {
2982
3095
  buildTaskCompletionTimeline,
2983
3096
  createAdapter,
2984
3097
  createPTYManager,
3098
+ ensurePty,
2985
3099
  extractTaskCompletionTraceRecords,
2986
3100
  isBun
2987
3101
  });