shellpuppet 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +31 -0
  3. package/README.md +499 -8
  4. package/dist/cli/desktopShortcut.d.ts +22 -0
  5. package/dist/cli/desktopShortcut.js +157 -0
  6. package/dist/cli/desktopShortcut.js.map +1 -0
  7. package/dist/cli/index.d.ts +13 -0
  8. package/dist/cli/index.js +846 -0
  9. package/dist/cli/index.js.map +1 -0
  10. package/dist/client/http.d.ts +143 -0
  11. package/dist/client/http.js +581 -0
  12. package/dist/client/http.js.map +1 -0
  13. package/dist/core/dialogs.d.ts +96 -0
  14. package/dist/core/dialogs.js +226 -0
  15. package/dist/core/dialogs.js.map +1 -0
  16. package/dist/core/echo.d.ts +71 -0
  17. package/dist/core/echo.js +249 -0
  18. package/dist/core/echo.js.map +1 -0
  19. package/dist/core/find.d.ts +34 -0
  20. package/dist/core/find.js +41 -0
  21. package/dist/core/find.js.map +1 -0
  22. package/dist/core/keys.d.ts +22 -0
  23. package/dist/core/keys.js +80 -0
  24. package/dist/core/keys.js.map +1 -0
  25. package/dist/core/manager.d.ts +96 -0
  26. package/dist/core/manager.js +271 -0
  27. package/dist/core/manager.js.map +1 -0
  28. package/dist/core/nativePty.d.ts +73 -0
  29. package/dist/core/nativePty.js +161 -0
  30. package/dist/core/nativePty.js.map +1 -0
  31. package/dist/core/policy.d.ts +61 -0
  32. package/dist/core/policy.js +143 -0
  33. package/dist/core/policy.js.map +1 -0
  34. package/dist/core/promptDetector.d.ts +109 -0
  35. package/dist/core/promptDetector.js +897 -0
  36. package/dist/core/promptDetector.js.map +1 -0
  37. package/dist/core/reasoning.d.ts +265 -0
  38. package/dist/core/reasoning.js +933 -0
  39. package/dist/core/reasoning.js.map +1 -0
  40. package/dist/core/regexBudget.d.ts +64 -0
  41. package/dist/core/regexBudget.js +103 -0
  42. package/dist/core/regexBudget.js.map +1 -0
  43. package/dist/core/renderer.d.ts +127 -0
  44. package/dist/core/renderer.js +316 -0
  45. package/dist/core/renderer.js.map +1 -0
  46. package/dist/core/selection.d.ts +6 -0
  47. package/dist/core/selection.js +48 -0
  48. package/dist/core/selection.js.map +1 -0
  49. package/dist/core/session.d.ts +565 -0
  50. package/dist/core/session.js +1450 -0
  51. package/dist/core/session.js.map +1 -0
  52. package/dist/core/types.d.ts +316 -0
  53. package/dist/core/types.js +71 -0
  54. package/dist/core/types.js.map +1 -0
  55. package/dist/core/waiter.d.ts +94 -0
  56. package/dist/core/waiter.js +310 -0
  57. package/dist/core/waiter.js.map +1 -0
  58. package/dist/core/windowsConsoleHost.d.ts +147 -0
  59. package/dist/core/windowsConsoleHost.js +588 -0
  60. package/dist/core/windowsConsoleHost.js.map +1 -0
  61. package/dist/core/windowsPtyStart.d.ts +161 -0
  62. package/dist/core/windowsPtyStart.js +236 -0
  63. package/dist/core/windowsPtyStart.js.map +1 -0
  64. package/dist/daemon/audit.d.ts +23 -0
  65. package/dist/daemon/audit.js +51 -0
  66. package/dist/daemon/audit.js.map +1 -0
  67. package/dist/daemon/auth.d.ts +197 -0
  68. package/dist/daemon/auth.js +366 -0
  69. package/dist/daemon/auth.js.map +1 -0
  70. package/dist/daemon/config.d.ts +119 -0
  71. package/dist/daemon/config.js +568 -0
  72. package/dist/daemon/config.js.map +1 -0
  73. package/dist/daemon/folderPicker.d.ts +27 -0
  74. package/dist/daemon/folderPicker.js +115 -0
  75. package/dist/daemon/folderPicker.js.map +1 -0
  76. package/dist/daemon/index.d.ts +7 -0
  77. package/dist/daemon/index.js +88 -0
  78. package/dist/daemon/index.js.map +1 -0
  79. package/dist/daemon/launchProfiles.d.ts +155 -0
  80. package/dist/daemon/launchProfiles.js +554 -0
  81. package/dist/daemon/launchProfiles.js.map +1 -0
  82. package/dist/daemon/liveness.d.ts +36 -0
  83. package/dist/daemon/liveness.js +71 -0
  84. package/dist/daemon/liveness.js.map +1 -0
  85. package/dist/daemon/logger.d.ts +7 -0
  86. package/dist/daemon/logger.js +29 -0
  87. package/dist/daemon/logger.js.map +1 -0
  88. package/dist/daemon/meta.d.ts +9 -0
  89. package/dist/daemon/meta.js +24 -0
  90. package/dist/daemon/meta.js.map +1 -0
  91. package/dist/daemon/paths.d.ts +85 -0
  92. package/dist/daemon/paths.js +154 -0
  93. package/dist/daemon/paths.js.map +1 -0
  94. package/dist/daemon/screenshot.d.ts +144 -0
  95. package/dist/daemon/screenshot.js +402 -0
  96. package/dist/daemon/screenshot.js.map +1 -0
  97. package/dist/daemon/server.d.ts +453 -0
  98. package/dist/daemon/server.js +3954 -0
  99. package/dist/daemon/server.js.map +1 -0
  100. package/dist/daemon/sessionFeed.d.ts +48 -0
  101. package/dist/daemon/sessionFeed.js +101 -0
  102. package/dist/daemon/sessionFeed.js.map +1 -0
  103. package/dist/daemon/terminalReports.d.ts +38 -0
  104. package/dist/daemon/terminalReports.js +84 -0
  105. package/dist/daemon/terminalReports.js.map +1 -0
  106. package/dist/daemon/viewerLauncher.d.ts +226 -0
  107. package/dist/daemon/viewerLauncher.js +562 -0
  108. package/dist/daemon/viewerLauncher.js.map +1 -0
  109. package/dist/daemon/viewerSync.d.ts +38 -0
  110. package/dist/daemon/viewerSync.js +83 -0
  111. package/dist/daemon/viewerSync.js.map +1 -0
  112. package/dist/mcp/config.d.ts +50 -0
  113. package/dist/mcp/config.js +95 -0
  114. package/dist/mcp/config.js.map +1 -0
  115. package/dist/mcp/index.d.ts +10 -0
  116. package/dist/mcp/index.js +78 -0
  117. package/dist/mcp/index.js.map +1 -0
  118. package/dist/mcp/tools.d.ts +66 -0
  119. package/dist/mcp/tools.js +638 -0
  120. package/dist/mcp/tools.js.map +1 -0
  121. package/dist/viewer/dashboard-folders.js +20 -0
  122. package/dist/viewer/dashboard-layout.js +52 -0
  123. package/dist/viewer/dashboard.css +67 -0
  124. package/dist/viewer/dashboard.html +44 -0
  125. package/dist/viewer/dashboard.js +1518 -0
  126. package/dist/viewer/icon-16.png +0 -0
  127. package/dist/viewer/icon-180.png +0 -0
  128. package/dist/viewer/icon-32.png +0 -0
  129. package/dist/viewer/icon-512.png +0 -0
  130. package/dist/viewer/icon.ico +0 -0
  131. package/dist/viewer/icon.svg +66 -0
  132. package/dist/viewer/index.html +61 -0
  133. package/dist/viewer/pane.js +736 -0
  134. package/dist/viewer/viewer.css +95 -0
  135. package/dist/viewer/viewer.js +84 -0
  136. package/package.json +66 -7
  137. package/index.js +0 -4
@@ -0,0 +1,161 @@
1
+ /**
2
+ * A Windows start that fails part-way releases what node-pty had built.
3
+ *
4
+ * node-pty 1.2.0-beta.18 builds a Windows terminal in steps
5
+ * (`lib/windowsPtyAgent.js`): it creates the pseudoconsole, which is a
6
+ * `conhost.exe` and two pipes; it starts a worker thread that connects to the
7
+ * output pipe; it arms a five-second timer and records a pending connection;
8
+ * and only then does it open the input pipe. Creating the *program* is
9
+ * deferred: `conptyNative.connect()` runs from the worker's 'ready' callback,
10
+ * so `ConnectNamedPipe` never blocks the event loop (upstream #763), and a
11
+ * connect that fails, late, no longer throws out of the constructor: the
12
+ * agent records an exit code, releases everything it built and reports the
13
+ * failure through `onError`, which the terminal turns into an 'exit' with
14
+ * that code (upstream #934, #935). A worker that errors or exits before it
15
+ * connects is heard the same way.
16
+ *
17
+ * What can still throw inside the constructor is the input pipe's open, which
18
+ * fails on every start under a sandbox that denies pipe access, after the
19
+ * worker has started and the deferred connection has been armed. Left alone,
20
+ * a worker that becomes ready afterwards would run the deferred `connect` and
21
+ * spawn the program of a start whose caller was told it failed, and the
22
+ * connection's own failure paths would then reach sockets that were never
23
+ * built. So `releaseFailedStarts` wraps node-pty's Windows agent so that a
24
+ * failed start cancels the deferred connection, gives the worker's error a
25
+ * listener, ends the worker, destroys both sockets and closes the
26
+ * pseudoconsole, before the error reaches the caller. The wrapper reads
27
+ * node-pty's private fields, so it wraps version 1.2.0-beta.18 only and
28
+ * leaves any other alone.
29
+ *
30
+ * The same wrapper makes a kill end no process of its own
31
+ * (`listNothingToKill`): node-pty would look them up by a pid the program may
32
+ * no longer hold. It drops a resize that arrives once node-pty has seen the
33
+ * program end, where node-pty would throw `Cannot resize a pty that has
34
+ * already exited`. node-pty holds a resize asked for before the terminal's
35
+ * first output and runs it from the output socket's first 'data' event, so
36
+ * when the program has ended by then, as it can on a busy daemon, that throw
37
+ * reaches no caller and would be an uncaught exception. Once the program has
38
+ * ended a resize changes nothing the session shows, so dropping the call
39
+ * loses nothing; a resize asked for after the end is refused by `Session.resize`
40
+ * instead, to its caller. Through `listenForTerminalErrors` it hears the
41
+ * errors node-pty leaves unheard on a live terminal's streams: the input
42
+ * socket, which nothing listens to at all, and the output socket, whose own
43
+ * handler rethrows any error but EIO while it is the socket's only 'error'
44
+ * listener. The worker's own errors node-pty now takes itself before it
45
+ * connects; after that they are swallowed, and the listener here keeps them
46
+ * recorded. `recordedExitCode` reads whether node-pty has seen a terminal's
47
+ * program end, where `canReadExitCode` says it can.
48
+ */
49
+ /** The fields of node-pty's `WindowsPtyAgent` that a failed start leaves set. */
50
+ export interface HalfBuiltAgent {
51
+ _pty?: number;
52
+ _useConptyDll?: boolean;
53
+ _ptyNative?: {
54
+ kill(pty: number, useConptyDll: boolean): void;
55
+ };
56
+ _inSocket?: {
57
+ destroy(): void;
58
+ };
59
+ _outSocket?: {
60
+ on(event: 'error', listener: () => void): unknown;
61
+ destroy(): void;
62
+ };
63
+ _conoutSocketWorker?: {
64
+ _worker?: {
65
+ on(event: 'error', listener: () => void): unknown;
66
+ terminate(): unknown;
67
+ };
68
+ };
69
+ /** node-pty's deferred `connect`; undefined once it has run, failed or been cancelled. */
70
+ _pendingPtyInfo?: unknown;
71
+ /** The timer that fails the connection when the output worker never connects. */
72
+ _connectionTimeout?: ReturnType<typeof setTimeout> | undefined;
73
+ }
74
+ /** The fields of node-pty's agent that its resize reads. */
75
+ interface ResizingAgent {
76
+ /** Set by node-pty's exit thread as it sees the terminal's program end. */
77
+ _exitCode?: number;
78
+ }
79
+ /** The methods of node-pty's agent that the wrapper replaces. */
80
+ interface AgentPrototype {
81
+ /** Where a kill takes its list of pids from. */
82
+ _getConsoleProcessList?: () => Promise<number[]>;
83
+ resize?: (this: ResizingAgent, cols: number, rows: number) => void;
84
+ }
85
+ type AgentConstructor = ((this: HalfBuiltAgent, ...args: unknown[]) => void) & {
86
+ prototype: AgentPrototype;
87
+ releasesFailedStarts?: true;
88
+ };
89
+ /** The shape of node-pty's `lib/windowsPtyAgent.js` exports that matter here. */
90
+ export interface AgentModule {
91
+ WindowsPtyAgent: AgentConstructor;
92
+ }
93
+ /**
94
+ * Replace the module's `WindowsPtyAgent` with one that releases a failed
95
+ * start, whose kill ends no process of its own (`listNothingToKill`) and whose
96
+ * resize is dropped once node-pty has seen the program end. node-pty looks the
97
+ * constructor up on this module object each time it builds a terminal, so the
98
+ * replacement takes effect for every later start.
99
+ */
100
+ export declare function wrapWindowsPtyAgent(agents: AgentModule): void;
101
+ /**
102
+ * Wrap the installed node-pty's Windows agent, once. True when it is wrapped;
103
+ * false when the installed version is not the one this was written against.
104
+ */
105
+ export declare function releaseFailedStarts(): boolean;
106
+ /**
107
+ * Take the errors of the streams of a live Windows terminal that node-pty
108
+ * 1.2.0-beta.18 leaves unheard, each of which would otherwise be an uncaught
109
+ * exception, on which the daemon shuts down with every session.
110
+ *
111
+ * - `input`: input reaches the pseudoconsole through a socket on its input
112
+ * pipe (`lib/windowsPtyAgent.js`), and nothing listens for its errors.
113
+ * Once the console host has closed its end, a write the pipe refuses
114
+ * (`write EAGAIN`) is emitted as an 'error'; a viewer's focus report racing
115
+ * the session's exit does it.
116
+ * - `output`: a worker thread copies the output pipe to a local socket
117
+ * (`lib/worker/conoutSocketWorker.js`), and an error thrown there becomes
118
+ * the worker's 'error'. node-pty's own listener takes it only while the
119
+ * worker has not yet connected; after that it is swallowed, so the listener
120
+ * here is what records it. The terminal reads that socket, and listens on
121
+ * it only once it has connected; its handler then throws any error but EIO
122
+ * while it is the socket's only 'error' listener (`lib/windowsTerminal.js`).
123
+ * The terminal's own `on('error')`, which its typings leave out, adds the
124
+ * second.
125
+ *
126
+ * With the listener the stream fails as before and the daemon and its other
127
+ * sessions carry on. False when the installed node-pty is not the wrapped
128
+ * version or the terminal has none of these.
129
+ */
130
+ export declare function listenForTerminalErrors(pty: unknown, listener: (error: Error, stream: 'input' | 'output') => void): boolean;
131
+ /**
132
+ * Whether node-pty records when this Windows terminal's program ends, so that
133
+ * `recordedExitCode` can say: node-pty 1.2.0-beta.18, which is ConPTY only
134
+ * and whose exit thread records the program's code as it sees the program
135
+ * end. Not with another node-pty version.
136
+ */
137
+ export declare function canReadExitCode(pty: unknown): boolean;
138
+ /**
139
+ * The exit code node-pty has recorded for a Windows terminal's program, or
140
+ * undefined while it has recorded none: while the program runs, and wherever
141
+ * `canReadExitCode` is false. node-pty holds the program's handle while the
142
+ * program runs and closes it as it sees the program end, a moment before the
143
+ * code is recorded here (`src/win/conpty.cc`). A kill in that moment can reach
144
+ * a pid just freed: the same kind of race every kill has while `taskkill`
145
+ * enumerates the processes. A connection that failed records a code too, with
146
+ * nothing ever spawned: no pid is walked for it.
147
+ */
148
+ export declare function recordedExitCode(pty: unknown): number | undefined;
149
+ /** node-pty exposes no error code for this bound in beta.18. Its agent's
150
+ * error event is the only way to distinguish it from other pid-zero exits.
151
+ * The exact text is pinned to the installed release, not a public API.
152
+ */
153
+ export declare function observeConptyStartError(pty: unknown, listener: (error: Error) => void): void;
154
+ /**
155
+ * Wrap the agent module only for the node-pty version whose private fields
156
+ * `release` reads. Another version is left alone and its module not loaded:
157
+ * wrapping internals nobody has read could break every start, where leaving
158
+ * them alone costs only the release.
159
+ */
160
+ export declare function wrapIfKnownVersion(version: string, load: () => AgentModule): boolean;
161
+ export {};
@@ -0,0 +1,236 @@
1
+ /**
2
+ * A Windows start that fails part-way releases what node-pty had built.
3
+ *
4
+ * node-pty 1.2.0-beta.18 builds a Windows terminal in steps
5
+ * (`lib/windowsPtyAgent.js`): it creates the pseudoconsole, which is a
6
+ * `conhost.exe` and two pipes; it starts a worker thread that connects to the
7
+ * output pipe; it arms a five-second timer and records a pending connection;
8
+ * and only then does it open the input pipe. Creating the *program* is
9
+ * deferred: `conptyNative.connect()` runs from the worker's 'ready' callback,
10
+ * so `ConnectNamedPipe` never blocks the event loop (upstream #763), and a
11
+ * connect that fails, late, no longer throws out of the constructor: the
12
+ * agent records an exit code, releases everything it built and reports the
13
+ * failure through `onError`, which the terminal turns into an 'exit' with
14
+ * that code (upstream #934, #935). A worker that errors or exits before it
15
+ * connects is heard the same way.
16
+ *
17
+ * What can still throw inside the constructor is the input pipe's open, which
18
+ * fails on every start under a sandbox that denies pipe access, after the
19
+ * worker has started and the deferred connection has been armed. Left alone,
20
+ * a worker that becomes ready afterwards would run the deferred `connect` and
21
+ * spawn the program of a start whose caller was told it failed, and the
22
+ * connection's own failure paths would then reach sockets that were never
23
+ * built. So `releaseFailedStarts` wraps node-pty's Windows agent so that a
24
+ * failed start cancels the deferred connection, gives the worker's error a
25
+ * listener, ends the worker, destroys both sockets and closes the
26
+ * pseudoconsole, before the error reaches the caller. The wrapper reads
27
+ * node-pty's private fields, so it wraps version 1.2.0-beta.18 only and
28
+ * leaves any other alone.
29
+ *
30
+ * The same wrapper makes a kill end no process of its own
31
+ * (`listNothingToKill`): node-pty would look them up by a pid the program may
32
+ * no longer hold. It drops a resize that arrives once node-pty has seen the
33
+ * program end, where node-pty would throw `Cannot resize a pty that has
34
+ * already exited`. node-pty holds a resize asked for before the terminal's
35
+ * first output and runs it from the output socket's first 'data' event, so
36
+ * when the program has ended by then, as it can on a busy daemon, that throw
37
+ * reaches no caller and would be an uncaught exception. Once the program has
38
+ * ended a resize changes nothing the session shows, so dropping the call
39
+ * loses nothing; a resize asked for after the end is refused by `Session.resize`
40
+ * instead, to its caller. Through `listenForTerminalErrors` it hears the
41
+ * errors node-pty leaves unheard on a live terminal's streams: the input
42
+ * socket, which nothing listens to at all, and the output socket, whose own
43
+ * handler rethrows any error but EIO while it is the socket's only 'error'
44
+ * listener. The worker's own errors node-pty now takes itself before it
45
+ * connects; after that they are swallowed, and the listener here keeps them
46
+ * recorded. `recordedExitCode` reads whether node-pty has seen a terminal's
47
+ * program end, where `canReadExitCode` says it can.
48
+ */
49
+ import { createRequire } from 'node:module';
50
+ /** The node-pty version whose private fields this relies on. */
51
+ const WRAPPED_VERSION = '1.2.0-beta.18';
52
+ /**
53
+ * Replace the module's `WindowsPtyAgent` with one that releases a failed
54
+ * start, whose kill ends no process of its own (`listNothingToKill`) and whose
55
+ * resize is dropped once node-pty has seen the program end. node-pty looks the
56
+ * constructor up on this module object each time it builds a terminal, so the
57
+ * replacement takes effect for every later start.
58
+ */
59
+ export function wrapWindowsPtyAgent(agents) {
60
+ const Original = agents.WindowsPtyAgent;
61
+ if (Original.releasesFailedStarts === true)
62
+ return;
63
+ const Wrapped = function WindowsPtyAgent(...args) {
64
+ const agent = Object.create(Original.prototype);
65
+ try {
66
+ Original.apply(agent, args);
67
+ }
68
+ catch (error) {
69
+ release(agent);
70
+ throw error;
71
+ }
72
+ return agent;
73
+ };
74
+ Wrapped.prototype = Original.prototype;
75
+ Wrapped.releasesFailedStarts = true;
76
+ agents.WindowsPtyAgent = Wrapped;
77
+ // node-pty's kill and resize look their methods up on the prototype, which
78
+ // the wrapped constructor shares, so every terminal from here on uses these.
79
+ if (typeof Original.prototype._getConsoleProcessList === 'function') {
80
+ Original.prototype._getConsoleProcessList = listNothingToKill;
81
+ }
82
+ const resize = Original.prototype.resize;
83
+ if (typeof resize === 'function') {
84
+ Original.prototype.resize = function resizeWhileRunning(cols, rows) {
85
+ if (this._exitCode !== undefined)
86
+ return;
87
+ resize.call(this, cols, rows);
88
+ };
89
+ }
90
+ }
91
+ /**
92
+ * The pids node-pty 1.2.0-beta.18's kill ends one by one
93
+ * (`lib/windowsPtyAgent.js`): none, found without starting anything.
94
+ *
95
+ * node-pty forks a helper that attaches to the console of whichever process
96
+ * now holds the program's pid and lists the processes on it, and with no
97
+ * answer waits five seconds and returns that pid itself; it then kills every
98
+ * pid in the list. It lists nothing while the deferred connection has not
99
+ * run, but once the program has started and ended, Windows is free to give
100
+ * its pid to another process, so either way the kill can end processes that
101
+ * are not the session's, as many as a whole console of them.
102
+ *
103
+ * A session's processes are ended by its tree kill instead (`session.ts`):
104
+ * every kill of a session whose program has not been seen to end runs
105
+ * `taskkill /T /F` from the program's pid, before node-pty's kill, and a
106
+ * program that has ended has nothing left for the helper to find. This
107
+ * replaces `_getConsoleProcessList` only, which the kill calls. node-pty's
108
+ * kill still closes the pseudoconsole while it holds the terminal, which it
109
+ * does until the program ends, so a program a tree kill left running is sent
110
+ * CTRL_CLOSE_EVENT with the rest of its console.
111
+ */
112
+ function listNothingToKill() {
113
+ return Promise.resolve([]);
114
+ }
115
+ /** Undo what a failed start built, as far as node-pty lets us. */
116
+ function release(agent) {
117
+ // Cancel the deferred connection first, so a worker that becomes ready after
118
+ // this cannot run it and spawn the program of a start that already failed,
119
+ // and the five-second timer cannot fire into a half-built terminal either.
120
+ agent._pendingPtyInfo = undefined;
121
+ if (agent._connectionTimeout !== undefined)
122
+ clearTimeout(agent._connectionTimeout);
123
+ const worker = agent._conoutSocketWorker?._worker;
124
+ if (worker !== undefined) {
125
+ // The worker's connect may still fail after this, and an 'error' with no
126
+ // listener would reach the daemon as an uncaught exception.
127
+ worker.on('error', () => undefined);
128
+ void worker.terminate();
129
+ }
130
+ // The input socket exists only when the input pipe could be opened.
131
+ agent._inSocket?.destroy();
132
+ // A 'ready' from the worker arriving after this would reconnect the output
133
+ // socket (connecting revives a destroyed socket), and its error would have
134
+ // no listener either.
135
+ agent._outSocket?.on('error', () => undefined);
136
+ agent._outSocket?.destroy();
137
+ if (agent._pty !== undefined && agent._ptyNative !== undefined) {
138
+ try {
139
+ agent._ptyNative.kill(agent._pty, agent._useConptyDll ?? false);
140
+ }
141
+ catch {
142
+ // Best effort: the start has already failed and its caller is told.
143
+ }
144
+ }
145
+ }
146
+ let wrapped = false;
147
+ /**
148
+ * Wrap the installed node-pty's Windows agent, once. True when it is wrapped;
149
+ * false when the installed version is not the one this was written against.
150
+ */
151
+ export function releaseFailedStarts() {
152
+ if (wrapped)
153
+ return true;
154
+ const require = createRequire(import.meta.url);
155
+ const { version } = require('node-pty/package.json');
156
+ wrapped = wrapIfKnownVersion(version, () => require('node-pty/lib/windowsPtyAgent.js'));
157
+ return wrapped;
158
+ }
159
+ /**
160
+ * Take the errors of the streams of a live Windows terminal that node-pty
161
+ * 1.2.0-beta.18 leaves unheard, each of which would otherwise be an uncaught
162
+ * exception, on which the daemon shuts down with every session.
163
+ *
164
+ * - `input`: input reaches the pseudoconsole through a socket on its input
165
+ * pipe (`lib/windowsPtyAgent.js`), and nothing listens for its errors.
166
+ * Once the console host has closed its end, a write the pipe refuses
167
+ * (`write EAGAIN`) is emitted as an 'error'; a viewer's focus report racing
168
+ * the session's exit does it.
169
+ * - `output`: a worker thread copies the output pipe to a local socket
170
+ * (`lib/worker/conoutSocketWorker.js`), and an error thrown there becomes
171
+ * the worker's 'error'. node-pty's own listener takes it only while the
172
+ * worker has not yet connected; after that it is swallowed, so the listener
173
+ * here is what records it. The terminal reads that socket, and listens on
174
+ * it only once it has connected; its handler then throws any error but EIO
175
+ * while it is the socket's only 'error' listener (`lib/windowsTerminal.js`).
176
+ * The terminal's own `on('error')`, which its typings leave out, adds the
177
+ * second.
178
+ *
179
+ * With the listener the stream fails as before and the daemon and its other
180
+ * sessions carry on. False when the installed node-pty is not the wrapped
181
+ * version or the terminal has none of these.
182
+ */
183
+ export function listenForTerminalErrors(pty, listener) {
184
+ if (!wrapped)
185
+ return false;
186
+ const terminal = pty;
187
+ const input = terminal._agent?.inSocket;
188
+ const worker = terminal._agent?._conoutSocketWorker?._worker;
189
+ input?.on('error', (error) => listener(error, 'input'));
190
+ worker?.on('error', (error) => listener(error, 'output'));
191
+ terminal.on?.('error', (error) => listener(error, 'output'));
192
+ return input !== undefined || worker !== undefined || terminal.on !== undefined;
193
+ }
194
+ /**
195
+ * Whether node-pty records when this Windows terminal's program ends, so that
196
+ * `recordedExitCode` can say: node-pty 1.2.0-beta.18, which is ConPTY only
197
+ * and whose exit thread records the program's code as it sees the program
198
+ * end. Not with another node-pty version.
199
+ */
200
+ export function canReadExitCode(pty) {
201
+ return wrapped && pty._agent !== undefined;
202
+ }
203
+ /**
204
+ * The exit code node-pty has recorded for a Windows terminal's program, or
205
+ * undefined while it has recorded none: while the program runs, and wherever
206
+ * `canReadExitCode` is false. node-pty holds the program's handle while the
207
+ * program runs and closes it as it sees the program end, a moment before the
208
+ * code is recorded here (`src/win/conpty.cc`). A kill in that moment can reach
209
+ * a pid just freed: the same kind of race every kill has while `taskkill`
210
+ * enumerates the processes. A connection that failed records a code too, with
211
+ * nothing ever spawned: no pid is walked for it.
212
+ */
213
+ export function recordedExitCode(pty) {
214
+ return canReadExitCode(pty) ? pty._agent?.exitCode : undefined;
215
+ }
216
+ /** node-pty exposes no error code for this bound in beta.18. Its agent's
217
+ * error event is the only way to distinguish it from other pid-zero exits.
218
+ * The exact text is pinned to the installed release, not a public API.
219
+ */
220
+ export function observeConptyStartError(pty, listener) {
221
+ const agent = pty._agent;
222
+ agent?.onError?.(listener);
223
+ }
224
+ /**
225
+ * Wrap the agent module only for the node-pty version whose private fields
226
+ * `release` reads. Another version is left alone and its module not loaded:
227
+ * wrapping internals nobody has read could break every start, where leaving
228
+ * them alone costs only the release.
229
+ */
230
+ export function wrapIfKnownVersion(version, load) {
231
+ if (version !== WRAPPED_VERSION)
232
+ return false;
233
+ wrapWindowsPtyAgent(load());
234
+ return true;
235
+ }
236
+ //# sourceMappingURL=windowsPtyStart.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"windowsPtyStart.js","sourceRoot":"","sources":["../../src/core/windowsPtyStart.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,OAAO,EAAE,aAAa,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Ae,CAAC,GAAY;IAC1C,OAAO,OAAO,IAAK,GAA6B,CAAC,MAAM,KAAK,SAAS,CAAC;AACxE,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAY;IAC3C,OAAO,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,GAA6B,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAC5F,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,GAAY,EAAE,QAAgC;IACpF,MAAM,KAAK,GAAI,GAAgF,CAAC,MAAM,CAAC;IACvG,KAAK,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC,CAAC;AAC7B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAe,EAAE,IAAuB;IACzE,IAAI,OAAO,KAAK,eAAe;QAAE,OAAO,KAAK,CAAC;IAC9C,mBAAmB,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5B,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Append-only action log at `~/.shellpuppet/audit.log` (design §4.5).
3
+ *
4
+ * THE RULE: typed content is never logged by default. Keystrokes are where the
5
+ * secrets are: an SSH passphrase or API key typed into a prompt must
6
+ * not end up in a plaintext log. Default entries record the action and the
7
+ * input LENGTH; the command line for `start` is recorded because commands are
8
+ * policy-visible by definition. Screen output is never logged.
9
+ *
10
+ * `audit.captureContent: true` in config.json opts into full input capture for
11
+ * debugging, and docs/reference/security-model.md documents it as secret-capturing.
12
+ */
13
+ import type { Logger } from '../core/types.js';
14
+ export interface Auditor {
15
+ readonly captureContent: boolean;
16
+ record(actor: string, action: string, session?: string, meta?: Record<string, unknown>): void;
17
+ }
18
+ export declare function auditLogPath(home: string): string;
19
+ /** An auditor that records nothing, used by unit-style tests. */
20
+ export declare const NULL_AUDITOR: Auditor;
21
+ export declare function createAuditor(home: string, options?: {
22
+ captureContent?: boolean;
23
+ }, logger?: Logger): Auditor;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Append-only action log at `~/.shellpuppet/audit.log` (design §4.5).
3
+ *
4
+ * THE RULE: typed content is never logged by default. Keystrokes are where the
5
+ * secrets are: an SSH passphrase or API key typed into a prompt must
6
+ * not end up in a plaintext log. Default entries record the action and the
7
+ * input LENGTH; the command line for `start` is recorded because commands are
8
+ * policy-visible by definition. Screen output is never logged.
9
+ *
10
+ * `audit.captureContent: true` in config.json opts into full input capture for
11
+ * debugging, and docs/reference/security-model.md documents it as secret-capturing.
12
+ */
13
+ import * as fs from 'node:fs';
14
+ import * as path from 'node:path';
15
+ import { NULL_LOGGER } from '../core/types.js';
16
+ export function auditLogPath(home) {
17
+ return path.join(home, 'audit.log');
18
+ }
19
+ /** An auditor that records nothing, used by unit-style tests. */
20
+ export const NULL_AUDITOR = {
21
+ captureContent: false,
22
+ record: () => undefined,
23
+ };
24
+ export function createAuditor(home, options = {}, logger = NULL_LOGGER) {
25
+ const file = auditLogPath(home);
26
+ let warned = false;
27
+ return {
28
+ captureContent: options.captureContent ?? false,
29
+ record(actor, action, session, meta) {
30
+ try {
31
+ const entry = {
32
+ time: new Date().toISOString(),
33
+ actor,
34
+ action,
35
+ ...(session !== undefined ? { session } : {}),
36
+ ...(meta !== undefined ? { meta } : {}),
37
+ };
38
+ fs.appendFileSync(file, JSON.stringify(entry) + '\n');
39
+ }
40
+ catch (error) {
41
+ if (!warned) {
42
+ warned = true;
43
+ logger.warn('audit log write failed: auditing disabled for this run', {
44
+ error: String(error),
45
+ });
46
+ }
47
+ }
48
+ },
49
+ };
50
+ }
51
+ //# sourceMappingURL=audit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audit.js","sourceRoot":"","sources":["../../src/daemon/audit.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,W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