shellpuppet 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +31 -0
  3. package/README.md +340 -8
  4. package/dist/cli/index.d.ts +13 -0
  5. package/dist/cli/index.js +693 -0
  6. package/dist/cli/index.js.map +1 -0
  7. package/dist/client/http.d.ts +82 -0
  8. package/dist/client/http.js +225 -0
  9. package/dist/client/http.js.map +1 -0
  10. package/dist/core/dialogs.d.ts +88 -0
  11. package/dist/core/dialogs.js +214 -0
  12. package/dist/core/dialogs.js.map +1 -0
  13. package/dist/core/echo.d.ts +71 -0
  14. package/dist/core/echo.js +249 -0
  15. package/dist/core/echo.js.map +1 -0
  16. package/dist/core/find.d.ts +34 -0
  17. package/dist/core/find.js +41 -0
  18. package/dist/core/find.js.map +1 -0
  19. package/dist/core/keys.d.ts +22 -0
  20. package/dist/core/keys.js +80 -0
  21. package/dist/core/keys.js.map +1 -0
  22. package/dist/core/manager.d.ts +66 -0
  23. package/dist/core/manager.js +158 -0
  24. package/dist/core/manager.js.map +1 -0
  25. package/dist/core/nativePty.d.ts +69 -0
  26. package/dist/core/nativePty.js +156 -0
  27. package/dist/core/nativePty.js.map +1 -0
  28. package/dist/core/policy.d.ts +61 -0
  29. package/dist/core/policy.js +143 -0
  30. package/dist/core/policy.js.map +1 -0
  31. package/dist/core/promptDetector.d.ts +75 -0
  32. package/dist/core/promptDetector.js +606 -0
  33. package/dist/core/promptDetector.js.map +1 -0
  34. package/dist/core/reasoning.d.ts +252 -0
  35. package/dist/core/reasoning.js +740 -0
  36. package/dist/core/reasoning.js.map +1 -0
  37. package/dist/core/regexBudget.d.ts +44 -0
  38. package/dist/core/regexBudget.js +79 -0
  39. package/dist/core/regexBudget.js.map +1 -0
  40. package/dist/core/renderer.d.ts +112 -0
  41. package/dist/core/renderer.js +225 -0
  42. package/dist/core/renderer.js.map +1 -0
  43. package/dist/core/session.d.ts +311 -0
  44. package/dist/core/session.js +966 -0
  45. package/dist/core/session.js.map +1 -0
  46. package/dist/core/types.d.ts +281 -0
  47. package/dist/core/types.js +47 -0
  48. package/dist/core/types.js.map +1 -0
  49. package/dist/core/waiter.d.ts +86 -0
  50. package/dist/core/waiter.js +317 -0
  51. package/dist/core/waiter.js.map +1 -0
  52. package/dist/core/windowsPtyStart.d.ts +90 -0
  53. package/dist/core/windowsPtyStart.js +129 -0
  54. package/dist/core/windowsPtyStart.js.map +1 -0
  55. package/dist/daemon/audit.d.ts +23 -0
  56. package/dist/daemon/audit.js +51 -0
  57. package/dist/daemon/audit.js.map +1 -0
  58. package/dist/daemon/auth.d.ts +127 -0
  59. package/dist/daemon/auth.js +241 -0
  60. package/dist/daemon/auth.js.map +1 -0
  61. package/dist/daemon/config.d.ts +76 -0
  62. package/dist/daemon/config.js +301 -0
  63. package/dist/daemon/config.js.map +1 -0
  64. package/dist/daemon/index.d.ts +7 -0
  65. package/dist/daemon/index.js +87 -0
  66. package/dist/daemon/index.js.map +1 -0
  67. package/dist/daemon/liveness.d.ts +36 -0
  68. package/dist/daemon/liveness.js +71 -0
  69. package/dist/daemon/liveness.js.map +1 -0
  70. package/dist/daemon/logger.d.ts +7 -0
  71. package/dist/daemon/logger.js +29 -0
  72. package/dist/daemon/logger.js.map +1 -0
  73. package/dist/daemon/meta.d.ts +9 -0
  74. package/dist/daemon/meta.js +24 -0
  75. package/dist/daemon/meta.js.map +1 -0
  76. package/dist/daemon/paths.d.ts +83 -0
  77. package/dist/daemon/paths.js +154 -0
  78. package/dist/daemon/paths.js.map +1 -0
  79. package/dist/daemon/screenshot.d.ts +87 -0
  80. package/dist/daemon/screenshot.js +256 -0
  81. package/dist/daemon/screenshot.js.map +1 -0
  82. package/dist/daemon/server.d.ts +244 -0
  83. package/dist/daemon/server.js +2187 -0
  84. package/dist/daemon/server.js.map +1 -0
  85. package/dist/daemon/terminalReports.d.ts +38 -0
  86. package/dist/daemon/terminalReports.js +84 -0
  87. package/dist/daemon/terminalReports.js.map +1 -0
  88. package/dist/daemon/viewerLauncher.d.ts +135 -0
  89. package/dist/daemon/viewerLauncher.js +280 -0
  90. package/dist/daemon/viewerLauncher.js.map +1 -0
  91. package/dist/daemon/viewerSync.d.ts +38 -0
  92. package/dist/daemon/viewerSync.js +83 -0
  93. package/dist/daemon/viewerSync.js.map +1 -0
  94. package/dist/mcp/config.d.ts +57 -0
  95. package/dist/mcp/config.js +106 -0
  96. package/dist/mcp/config.js.map +1 -0
  97. package/dist/mcp/index.d.ts +10 -0
  98. package/dist/mcp/index.js +69 -0
  99. package/dist/mcp/index.js.map +1 -0
  100. package/dist/mcp/tools.d.ts +59 -0
  101. package/dist/mcp/tools.js +537 -0
  102. package/dist/mcp/tools.js.map +1 -0
  103. package/dist/viewer/icon-16.png +0 -0
  104. package/dist/viewer/icon-180.png +0 -0
  105. package/dist/viewer/icon-32.png +0 -0
  106. package/dist/viewer/icon-512.png +0 -0
  107. package/dist/viewer/icon.svg +66 -0
  108. package/dist/viewer/index.html +56 -0
  109. package/dist/viewer/viewer.css +84 -0
  110. package/dist/viewer/viewer.js +661 -0
  111. package/package.json +66 -7
  112. package/index.js +0 -4
@@ -0,0 +1,244 @@
1
+ /**
2
+ * The ShellPuppet daemon: one per user, owns every PTY, serves clients and
3
+ * viewers (design §2). Binds 127.0.0.1 on an OS-assigned port, publishes its
4
+ * discovery record atomically and defends the loopback surface (§4.2):
5
+ * Host-header validation on every request, Origin validation on WebSocket
6
+ * upgrades, bearer-token auth on the API, no CORS headers at all.
7
+ */
8
+ import type { Logger, PolicyConfig, SessionEnvMode } from '../core/types.js';
9
+ import { launchViewer } from './viewerLauncher.js';
10
+ import { captureViewer } from './screenshot.js';
11
+ import type { ViewerLaunchConfig } from './viewerLauncher.js';
12
+ import type { Auditor } from './audit.js';
13
+ import type { DaemonState } from './paths.js';
14
+ export interface DaemonOptions {
15
+ home: string;
16
+ logger: Logger;
17
+ policy?: PolicyConfig;
18
+ /** Session environment hygiene (design §4.6, ADR-008). Defaults to clean. */
19
+ sessionEnv?: SessionEnvMode;
20
+ /** Action audit log (design §4.5). Defaults to no auditing (tests). */
21
+ audit?: Auditor;
22
+ idleMs?: number;
23
+ /** Test hook: how often the idle check (and exited-session sweep) runs. */
24
+ idleCheckIntervalMs?: number;
25
+ /** Test hook: how often the daemon checks that daemon.json still names it. */
26
+ statePublishCheckMs?: number;
27
+ /** How long exited sessions stay readable before being reaped. */
28
+ exitedGraceMs?: number;
29
+ /** Called after an idle or lost-race self-stop; the entry point wires process.exit. */
30
+ onSelfStop?: (reason: string) => void;
31
+ /** Prompt-stall detection cadence. Test hooks; defaults above. */
32
+ promptScanIntervalMs?: number;
33
+ promptStallMs?: number;
34
+ /**
35
+ * How the viewer window is opened on session start. Absent means
36
+ * "none", a deliberately inert default, so a test daemon cannot put browser
37
+ * windows on someone's desktop by forgetting to opt out. The daemon entry
38
+ * point supplies the product default of `isolated-app`.
39
+ */
40
+ viewer?: ViewerLaunchConfig;
41
+ /**
42
+ * Test seam: what actually opens the window. Defaults to the real launcher.
43
+ * It exists so a test can see the URL the daemon would open, and check
44
+ * that the window gets a grant of its own rather than the caller's.
45
+ */
46
+ launcher?: typeof launchViewer;
47
+ /**
48
+ * Test seam: what photographs the page. Defaults to the real headless
49
+ * capture. It exists so a test can see the credential a capture is handed
50
+ * without starting a browser, and check that it may watch but never write.
51
+ */
52
+ capture?: typeof captureViewer;
53
+ }
54
+ /** Thrown by `start()` when a healthy daemon already owns this home. */
55
+ export declare class AlreadyRunningError extends Error {
56
+ readonly state: DaemonState;
57
+ constructor(state: DaemonState);
58
+ }
59
+ export declare class Daemon {
60
+ private readonly options;
61
+ private readonly logger;
62
+ private readonly tokens;
63
+ private readonly manager;
64
+ private readonly server;
65
+ private readonly wss;
66
+ private readonly sockets;
67
+ /** Every watcher of a session, with the gate that orders its stream across snapshots. */
68
+ private readonly sessionSockets;
69
+ /**
70
+ * The viewport currently driving each session's grid (§5).
71
+ *
72
+ * Connecting is passive and acting takes control: the first viewport to
73
+ * arrive with nobody driving picks the grid up, a later one adopts what it
74
+ * finds and any deliberate act by that human, such as resizing the window
75
+ * or changing the text size, hands control over. So there is always a way back
76
+ * from a grid somebody else chose, without a control that would mean nothing
77
+ * while a single viewer is the whole product.
78
+ */
79
+ private readonly geometryController;
80
+ /** Subscribers to the daemon-wide event stream (`/events`). */
81
+ private readonly eventSockets;
82
+ /**
83
+ * Per-session prompt tracking for stall alerts: the signature of the prompt
84
+ * currently on screen, when it was first seen and whether it has alerted.
85
+ */
86
+ private readonly promptStates;
87
+ private readonly version;
88
+ private portValue;
89
+ private waitsInFlight;
90
+ private lastActivity;
91
+ private idleTimer;
92
+ private raceCheckTimer;
93
+ private statePublishTimer;
94
+ private statePublishRunning;
95
+ /** What this daemon published in daemon.json, once it has claimed it. */
96
+ private publishedState;
97
+ /** The pid of another running daemon already reported as owning daemon.json. */
98
+ private foreignOwnerReported;
99
+ private promptScanTimer;
100
+ private promptScanRunning;
101
+ private stopPromise;
102
+ private readonly audit;
103
+ /**
104
+ * What sessions get from the daemon's environment (ADR-008), resolved once so
105
+ * that what the daemon reports is what its sessions get.
106
+ */
107
+ private readonly sessionEnv;
108
+ constructor(options: DaemonOptions);
109
+ get port(): number;
110
+ get token(): string;
111
+ get connectedSockets(): number;
112
+ /**
113
+ * How many waits are polling right now. A counter rather than a log line,
114
+ * because `core/` and `client/` may not write to a stream and a test needs
115
+ * to see that a wait whose caller has gone actually stopped.
116
+ */
117
+ get waitsPolling(): number;
118
+ get liveSessions(): number;
119
+ start(): Promise<{
120
+ port: number;
121
+ token: string;
122
+ }>;
123
+ /**
124
+ * Whether the daemon a record names is running, so that this one must
125
+ * defer to it: one that answers its health check, or one whose process is
126
+ * alive and whose port accepts a connection although it is too busy to
127
+ * answer. A record naming this very daemon is not an incumbent, and one
128
+ * naming this process that does not answer is a leftover from an earlier
129
+ * process that had the same pid.
130
+ */
131
+ private incumbentRunning;
132
+ /**
133
+ * Keep daemon.json naming this daemon while it runs. When the file is
134
+ * missing, holds no daemon record or names a daemon that has gone, publish
135
+ * it again, so new clients can still reach this daemon's sessions. When it
136
+ * names another daemon that is running, leave it, but say so once in
137
+ * daemon.log: new clients reach that daemon, and this one's sessions only
138
+ * through clients already connected. A file that cannot be read is left
139
+ * alone, since a failed read says nothing about what it holds.
140
+ */
141
+ private keepStatePublished;
142
+ private isHealthy;
143
+ private closeServerOnly;
144
+ /**
145
+ * Kill every session's process tree, close everything, remove the state
146
+ * file (unless we lost the race and it now belongs to the winner).
147
+ * The daemon must never exit and strand running agent CLIs (design §2.3).
148
+ */
149
+ stop(opts?: {
150
+ removeState?: boolean;
151
+ }): Promise<void>;
152
+ private doStop;
153
+ /**
154
+ * What sessions inherit (ADR-008): the mode, and under passthrough the names
155
+ * of the credential-shaped variables in the daemon's environment. Never values.
156
+ */
157
+ private environmentReport;
158
+ private hostAllowed;
159
+ private originAllowed;
160
+ private handleRequest;
161
+ private handleStart;
162
+ /**
163
+ * The signature that reads a session's screen for `hideReasoning` and
164
+ * `terminal_result`. Without one, a 400 is sent and null returned: those
165
+ * readers fail closed.
166
+ */
167
+ private readerFor;
168
+ /**
169
+ * Poll for the typed text to appear on screen, for longer the longer the
170
+ * text (VERIFY_MS_PER_CHAR). Matches on the last
171
+ * printable run of the input (up to 40 chars) so a trailing Enter or an
172
+ * embedded control sequence does not defeat the check. For input long
173
+ * enough to be lost in pieces, it matches on the FIRST run as well (see the
174
+ * needle comments below for the loss this exists to catch).
175
+ *
176
+ * Both needles must appear, read the way a TUI wraps typed text
177
+ * (`screenShows`): a long prompt is redrawn across the rows of an input
178
+ * box, and the line break, the continuation row's indent, the box's border
179
+ * and any side panel drawn on the same rows land in the middle of the
180
+ * needle. `before` is the screen as it was before the input: rows still the
181
+ * same are not read as wrapped pieces of it.
182
+ */
183
+ private verifyTyped;
184
+ /**
185
+ * Reap naturally-exited sessions once their grace lapses, releasing what a
186
+ * kill releases: the renderer, the session's viewer grants and credentials, and its
187
+ * watcher sockets, so an abandoned viewer tab cannot hold off idle shutdown.
188
+ */
189
+ private sweepExited;
190
+ /** Revoke a removed session's viewer grants and credentials and close its watcher sockets. */
191
+ private dropSessionArtifacts;
192
+ /** Whether a live viewport is currently driving this session's grid. */
193
+ private hasGeometryController;
194
+ /**
195
+ * Session list decorated with whatever prompt each session is holding. The
196
+ * stall alert also goes to `daemon.log` and the `/events` socket, but an
197
+ * agent reaches the daemon through request-and-answer tool calls or one-shot
198
+ * CLI runs, so it can never hold that socket: `terminal_list` is how a
199
+ * manager agent learns that its delegate is blocked, which is the whole
200
+ * point of detecting it.
201
+ *
202
+ * Read from the scanner's own state rather than re-detecting here: one
203
+ * source of truth means what the list reports and what the log announces
204
+ * cannot disagree, and a list call stays cheap (no render per session).
205
+ * The cost is staleness bounded by the scan interval: 5 s, immaterial
206
+ * against prompts that sit for minutes.
207
+ */
208
+ private listWithPrompts;
209
+ /**
210
+ * Scan every live session's screen for a permission prompt and alert when
211
+ * the SAME prompt has been waiting longer than the stall threshold, so an
212
+ * unserviced prompt does not cost the delegate tens of idle minutes until
213
+ * someone happens to snapshot the session.
214
+ */
215
+ private scanForPromptStalls;
216
+ /** Fan an event out to every `/events` subscriber (TEXT JSON frames). */
217
+ private emitEvent;
218
+ private handleUpgrade;
219
+ /** Attach a subscriber to the daemon-wide event stream. */
220
+ private attachEventSocket;
221
+ /** Send a JSON control message (TEXT frame) to every watcher of a session. */
222
+ private broadcastControl;
223
+ /**
224
+ * Tell every watcher the current grid and who drives it, then hand each one
225
+ * a snapshot at that size. Both halves matter: the frame moves their grid,
226
+ * and the snapshot repaints it from the daemon's renderer rather than from
227
+ * xterm's in-place reflow of what they had (§5).
228
+ */
229
+ private announceGeometry;
230
+ /** What one watcher is told about the grid, from its own point of view. */
231
+ private geometryFrame;
232
+ /** The viewer URL for a session, carrying a one-use grant. */
233
+ private viewerUrlFor;
234
+ /** The daemon's rendered state as viewer bytes: a wipe, then the rebuild. */
235
+ private snapshotForViewer;
236
+ /**
237
+ * Wire protocol: BINARY frames carry terminal bytes (snapshots, live mirror,
238
+ * exit notice); TEXT frames are JSON control messages. Incoming text frames
239
+ * are either JSON controls (resize) or bytes for the program: a person's
240
+ * keystrokes, or the viewer's terminal answering the program
241
+ * (terminalReports.ts).
242
+ */
243
+ private attachSocket;
244
+ }