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,453 @@
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 { Session } from '../core/session.js';
9
+ import type { Logger, PolicyConfig, SessionOptions, SessionEnvMode } from '../core/types.js';
10
+ import { launchViewer } from './viewerLauncher.js';
11
+ import { captureViewer } from './screenshot.js';
12
+ import type { ViewerLaunchConfig } from './viewerLauncher.js';
13
+ import type { Auditor } from './audit.js';
14
+ import type { DaemonState } from './paths.js';
15
+ import type { FeedFrame } from './sessionFeed.js';
16
+ import type { LauncherConfig } from './launchProfiles.js';
17
+ export interface DaemonOptions {
18
+ home: string;
19
+ logger: Logger;
20
+ startedWrapped?: boolean;
21
+ policy?: PolicyConfig;
22
+ /** Session environment hygiene (design §4.6, ADR-008). Defaults to clean. */
23
+ sessionEnv?: SessionEnvMode;
24
+ /** Action audit log (design §4.5). Defaults to no auditing (tests). */
25
+ audit?: Auditor;
26
+ idleMs?: number;
27
+ /** Test hook: how often the idle check (and exited-session sweep) runs. */
28
+ idleCheckIntervalMs?: number;
29
+ /** Test hook: how often the daemon checks that daemon.json still names it. */
30
+ statePublishCheckMs?: number;
31
+ /** How long exited sessions stay readable before being reaped. */
32
+ exitedGraceMs?: number;
33
+ /** Called after an idle or lost-race self-stop; the entry point wires process.exit. */
34
+ onSelfStop?: (reason: string) => void;
35
+ /** Prompt-stall detection cadence. Test hooks; defaults above. */
36
+ promptScanIntervalMs?: number;
37
+ promptStallMs?: number;
38
+ /**
39
+ * How the viewer window is opened on session start. Absent means
40
+ * "no-window", a deliberately inert default, so a test daemon cannot put browser
41
+ * windows on someone's desktop by forgetting to opt out. The daemon entry
42
+ * point supplies the product default of `dashboard`.
43
+ */
44
+ viewer?: ViewerLaunchConfig;
45
+ /** Initial dashboard text size, served to the page without a write route. */
46
+ dashboardFontSize?: number;
47
+ /** Test hooks for the dashboard event socket heartbeat. */
48
+ dashboardHeartbeatIntervalMs?: number;
49
+ dashboardHeartbeatTimeoutMs?: number;
50
+ /**
51
+ * Test seam: what actually opens the window. Defaults to the real launcher.
52
+ * It exists so a test can see the URL the daemon would open, and check
53
+ * that the window gets a grant of its own rather than the caller's.
54
+ */
55
+ launcher?: typeof launchViewer;
56
+ /**
57
+ * Test seam: what photographs the page. Defaults to the real headless
58
+ * capture. It exists so a test can see the credential a capture is handed
59
+ * without starting a browser, and check that it may watch but never write.
60
+ */
61
+ capture?: typeof captureViewer;
62
+ /**
63
+ * Test seam: the clock dashboard grant expiry is read from. Absent means
64
+ * `Date.now`. A request cannot choose a grant's lifetime.
65
+ */
66
+ now?: () => number;
67
+ /**
68
+ * The named programs the dashboard may start (`launcher.profiles`, design
69
+ * §4.3). Absent means a watch-only dashboard. Problems are served with the
70
+ * profile list and logged by the entry point at start.
71
+ */
72
+ launchProfiles?: LauncherConfig;
73
+ /** Test seam for dashboard profile environment copies. */
74
+ daemonEnv?: NodeJS.ProcessEnv;
75
+ /** Injected in tests so no desktop dialog appears. */
76
+ folderPicker?: (signal: AbortSignal) => Promise<string | null>;
77
+ /** Test hook for a picker that does not settle. */
78
+ folderPickerTimeoutMs?: number;
79
+ /** Test seam for dashboard starts without native terminal startup. */
80
+ dashboardSessionFactory?: (options: SessionOptions) => Session;
81
+ }
82
+ export declare class AlreadyRunningError extends Error {
83
+ readonly state: DaemonState;
84
+ constructor(state: DaemonState);
85
+ }
86
+ export declare class Daemon {
87
+ private readonly options;
88
+ private readonly logger;
89
+ private readonly tokens;
90
+ private readonly manager;
91
+ private readonly server;
92
+ private readonly wss;
93
+ private readonly sockets;
94
+ /** Every watcher of a session, with the gate that orders its stream across snapshots. */
95
+ private readonly sessionSockets;
96
+ /** Frozen replay bytes belong only to the screenshot's one-use viewer grant. */
97
+ private readonly captureSnapshots;
98
+ /**
99
+ * The viewport currently driving each session's grid (§5).
100
+ *
101
+ * Connecting is passive and acting takes control: the first single-session
102
+ * viewport to arrive with nobody driving picks the grid up, a later one
103
+ * adopts what it finds, and a resize from that human hands control over.
104
+ * A dashboard credential is the exception, whatever the query says. It
105
+ * adopts and does not pick the grid up on connect, and it does not inherit
106
+ * the grid when the controlling socket leaves. Its resize counts only when
107
+ * the frame carries `take: true`. `drive=0` is the same opt-out for a
108
+ * viewer or client socket, and it cannot make a dashboard socket drive.
109
+ */
110
+ private readonly geometryController;
111
+ /** A dashboard document keeps its grid while a covered pane reconnects. */
112
+ private readonly dashboardGeometryOwner;
113
+ /** Subscribers to the daemon-wide event stream (`/events`). */
114
+ private readonly eventSockets;
115
+ /**
116
+ * Open `/dashboard/events` sockets. While one is open, a session start does
117
+ * not launch a per-session viewer window and does not hand back a viewer URL.
118
+ */
119
+ private readonly dashboardEventSockets;
120
+ /** Opaque roster ids carried by dashboard feed sockets, never used for authorisation. */
121
+ private readonly dashboardWindowIds;
122
+ private readonly liveDashboardEventSockets;
123
+ /** A launched dashboard has 30 seconds to connect before a start may retry. */
124
+ private dashboardWindowStartedAt;
125
+ private dashboardOpenPromise;
126
+ private readonly pendingDashboardGrants;
127
+ private stopping;
128
+ /**
129
+ * Per-session prompt tracking for stall alerts: the signature of the prompt
130
+ * currently on screen, when it was first seen and whether it has alerted.
131
+ */
132
+ private readonly promptStates;
133
+ private readonly version;
134
+ private readonly sessionFeed;
135
+ private portValue;
136
+ private waitsInFlight;
137
+ private lastActivity;
138
+ private idleTimer;
139
+ private raceCheckTimer;
140
+ private statePublishTimer;
141
+ private statePublishRunning;
142
+ /** What this daemon published in daemon.json, once it has claimed it. */
143
+ private publishedState;
144
+ /** The pid of another running daemon already reported as owning daemon.json. */
145
+ private foreignOwnerReported;
146
+ private promptScanTimer;
147
+ private readonly promptGraceTimers;
148
+ private promptScanRunning;
149
+ private stopPromise;
150
+ private readonly audit;
151
+ /**
152
+ * What sessions get from the daemon's environment (ADR-008), resolved once so
153
+ * that what the daemon reports is what its sessions get.
154
+ */
155
+ private readonly sessionEnv;
156
+ /** The profiles the dashboard may name, and the entries that did not load. */
157
+ private readonly launchProfiles;
158
+ private readonly daemonEnv;
159
+ /** The global policy, kept here so a launch can report it before creating. */
160
+ private readonly compiledPolicy;
161
+ /** Accepted launch times, for the sliding sixty-second cap. */
162
+ private readonly launchTimes;
163
+ private folderPickerBusy;
164
+ private folderPickerAbort;
165
+ private folderPickerDone;
166
+ /** Includes requests still reading their body when shutdown begins. */
167
+ private readonly folderPickerHandlers;
168
+ /**
169
+ * Sessions created whose program does not exist yet (`programStarted`):
170
+ * not listed and not in the feed until the start contract is met.
171
+ */
172
+ private readonly startingIds;
173
+ constructor(options: DaemonOptions);
174
+ get port(): number;
175
+ /** Clock for dashboard grant expiry. Tests inject one; otherwise wall time. */
176
+ private now;
177
+ get token(): string;
178
+ get connectedSockets(): number;
179
+ /**
180
+ * How many waits are polling right now. A counter rather than a log line,
181
+ * because `core/` and `client/` may not write to a stream and a test needs
182
+ * to see that a wait whose caller has gone actually stopped.
183
+ */
184
+ get waitsPolling(): number;
185
+ get liveSessions(): number;
186
+ /** Subscribe inside the daemon to the dashboard's session feed. */
187
+ subscribeDashboardFeed(listener: (frame: FeedFrame) => void): () => void;
188
+ start(): Promise<{
189
+ port: number;
190
+ token: string;
191
+ }>;
192
+ /**
193
+ * Whether the daemon a record names is running, so that this one must
194
+ * defer to it: one that answers its health check, or one whose process is
195
+ * alive and whose port accepts a connection although it is too busy to
196
+ * answer. A record naming this very daemon is not an incumbent, and one
197
+ * naming this process that does not answer is a leftover from an earlier
198
+ * process that had the same pid.
199
+ */
200
+ private incumbentRunning;
201
+ /**
202
+ * Keep daemon.json naming this daemon while it runs. When the file is
203
+ * missing, holds no daemon record or names a daemon that has gone, publish
204
+ * it again, so new clients can still reach this daemon's sessions. When it
205
+ * names another daemon that is running, leave it, but say so once in
206
+ * daemon.log: new clients reach that daemon, and this one's sessions only
207
+ * through clients already connected. A file that cannot be read is left
208
+ * alone, since a failed read says nothing about what it holds.
209
+ */
210
+ private keepStatePublished;
211
+ private isHealthy;
212
+ private closeServerOnly;
213
+ /**
214
+ * Kill every session's process tree, close everything, remove the state
215
+ * file (unless we lost the race and it now belongs to the winner).
216
+ * The daemon must never exit and strand running agent CLIs (design §2.3).
217
+ */
218
+ stop(opts?: {
219
+ removeState?: boolean;
220
+ }): Promise<void>;
221
+ private doStop;
222
+ /**
223
+ * What sessions inherit (ADR-008): the mode, and under passthrough the names
224
+ * of the credential-shaped variables in the daemon's environment. Never values.
225
+ */
226
+ private environmentReport;
227
+ private hostAllowed;
228
+ private originAllowed;
229
+ /**
230
+ * An Origin that is this daemon's own and whose host matches `Host`.
231
+ * `127.0.0.1` and `localhost` are both allowed names, and they are not the
232
+ * same host: a cookie minted for one must not be presented as the other.
233
+ */
234
+ private originMatchesRequest;
235
+ /** Top-level document load of the dashboard. Origin may be absent. */
236
+ private isDocumentNavigation;
237
+ /** Same-origin read, such as `GET /sessions` from the dashboard page. */
238
+ private isSameOriginRead;
239
+ /** `same-site`, `cross-site`, or any site value that is not a dashboard site. */
240
+ private forbiddenFetchSite;
241
+ private dashboardCookiePresented;
242
+ /** The first `sp_dash` value the daemon issued for the current credential. */
243
+ private dashboardCookieToken;
244
+ private auditDashboardRefusal;
245
+ /**
246
+ * Why a presented cookie was refused on a route that may omit Origin:
247
+ * the dashboard page, outside a document navigation, and `GET /sessions`.
248
+ * A present Origin that does not match is `origin`. These routes allow an
249
+ * absent Origin, so that absence is not the reason: the Fetch Metadata is.
250
+ */
251
+ private cookieRefusalReason;
252
+ private dashboardDocument;
253
+ private sendDashboardPage;
254
+ /**
255
+ * `GET` and `HEAD /dashboard`. A grant is spent only by an accepted
256
+ * document navigation, which redirects with a cookie before any page loads.
257
+ * A fetch does not spend it. A bad, spent or absent grant without a live
258
+ * cookie returns the guidance page without revealing whether it was real.
259
+ */
260
+ private handleDashboardPage;
261
+ private openDashboardWindow;
262
+ private handleDashboardOpen;
263
+ private handleDesktopOpen;
264
+ private openDashboardForStart;
265
+ private writeSessionList;
266
+ private waitingSessions;
267
+ private waitAcrossSessions;
268
+ /** Cookie-authenticated `GET /sessions`. A missing or wrong proof is a 403. */
269
+ private serveDashboardSessionList;
270
+ /** A cookie mutation: same-origin Fetch Metadata and a matching Origin. */
271
+ private isSameOriginMutation;
272
+ /**
273
+ * Why a cookie mutation is refused, for the audit. A missing or foreign
274
+ * `Origin` is `origin`; a request whose `Origin` is this page's but whose
275
+ * `Sec-Fetch-Site` is not `same-origin` is `fetch-site`. The route needs
276
+ * both, so this only decides the reason a request failing both is recorded
277
+ * under: naming the Origin keeps the two defences apart in the log.
278
+ */
279
+ private mutationRefusalReason;
280
+ /**
281
+ * What the page needs to show a profile, and nothing secret: the command
282
+ * and arguments are the profile's own, and `envFromDaemon` contributes
283
+ * configured names or "all". `{folder}` stays unexpanded here because it
284
+ * depends on the click.
285
+ */
286
+ private profileDescription;
287
+ /** Cookie-authenticated `GET /dashboard/profiles`. A missing proof is a 403. */
288
+ private serveDashboardProfiles;
289
+ /** The desktop picker is available only to an authenticated any-folder profile. */
290
+ private handleDashboardFolderPicker;
291
+ /**
292
+ * The directory a launch starts in: the selected folder's configured path,
293
+ * the profile's fixed path, or the user's home. Relative paths are taken
294
+ * from the user's home, as a direct `POST /sessions` does. The path is a
295
+ * spawn `cwd`, never a command-line fragment.
296
+ */
297
+ private resolveLaunchCwd;
298
+ /** Accepted launch times inside the last sixty seconds, oldest first. */
299
+ private pruneLaunchTimes;
300
+ /**
301
+ * `POST /dashboard/launch`: one configured profile, and nothing else. The
302
+ * body names a profile and, when that profile asks, a folder. Only profiles
303
+ * configured for any folder may receive a path. The page cannot supply a
304
+ * command, arguments or an environment value.
305
+ */
306
+ private handleDashboardLaunch;
307
+ /**
308
+ * `DELETE /sessions/<id>` for the dashboard cookie. Origin, Fetch Metadata
309
+ * and the document proof are the same checks as launch. One call kills the
310
+ * session and audits actor `dashboard`. A client token never reaches here.
311
+ */
312
+ private handleDashboardKill;
313
+ /**
314
+ * Why a `/dashboard/events` upgrade is refused, or null when it may attach.
315
+ * The client token on the query is not a way in. The proof is the
316
+ * subprotocol, not `X-ShellPuppet-CSRF`.
317
+ */
318
+ private dashboardUpgradeRefusal;
319
+ /**
320
+ * Select `sp-csrf.<proof>` when this upgrade's credential is the dashboard
321
+ * cookie: the dashboard feed, or a session stream the client token did not
322
+ * take. The echo names that credential. A viewer or client socket, including
323
+ * a client token that also carries the cookie and offers a live proof,
324
+ * selects none. Returning false leaves the upgrade with none selected.
325
+ */
326
+ private dashboardProtocol;
327
+ /**
328
+ * Who may open `/sessions/<id>/stream`, or why not.
329
+ *
330
+ * A client token and a viewer cookie for this session keep the check they
331
+ * already have: a missing Origin is allowed, and a foreign one is refused.
332
+ * The dashboard cookie is a third way in, and only with the events
333
+ * upgrade's rules: a live `sp-csrf` subprotocol, a matching Origin, and
334
+ * same-origin Fetch Metadata. A header is not the proof. A viewer cookie,
335
+ * including a read-only one, is never treated as the dashboard credential.
336
+ * When both are present and the proof is live, the dashboard cookie is the
337
+ * one that opens the socket, so a read-only viewer cookie in the same jar
338
+ * does not mask it. When the proof is missing, a viewer cookie for this
339
+ * session still opens the socket on its own rules.
340
+ */
341
+ private sessionStreamDecision;
342
+ private handleRequest;
343
+ private handleStart;
344
+ /**
345
+ * The signature that reads a session's screen for `hideReasoning` and
346
+ * `terminal_result`. Without one, a 400 is sent and null returned: those
347
+ * readers fail closed.
348
+ */
349
+ private readerFor;
350
+ /**
351
+ * Poll for the typed text to appear on screen, for longer the longer the
352
+ * text (VERIFY_MS_PER_CHAR). Matches on the last
353
+ * printable run of the input (up to 40 chars) so a trailing Enter or an
354
+ * embedded control sequence does not defeat the check. For input long
355
+ * enough to be lost in pieces, it matches on the FIRST run as well (see the
356
+ * needle comments below for the loss this exists to catch).
357
+ *
358
+ * Both needles must appear, read the way a TUI wraps typed text
359
+ * (`screenShows`): a long prompt is redrawn across the rows of an input
360
+ * box, and the line break, the continuation row's indent, the box's border
361
+ * and any side panel drawn on the same rows land in the middle of the
362
+ * needle. `before` is the screen as it was before the input: rows still the
363
+ * same are not read as wrapped pieces of it.
364
+ */
365
+ private verifyTyped;
366
+ /**
367
+ * Reap naturally-exited sessions once their grace lapses, releasing what a
368
+ * kill releases: the renderer, the session's viewer grants and credentials, and its
369
+ * watcher sockets, so an abandoned viewer tab cannot hold off idle shutdown.
370
+ */
371
+ private sweepExited;
372
+ /** Revoke a removed session's viewer grants and credentials and close its watcher sockets. */
373
+ private dropSessionArtifacts;
374
+ /** Whether a live viewport is currently driving this session's grid. */
375
+ private hasGeometryController;
376
+ /**
377
+ * Wait, for a session just created, until its program exists (the start
378
+ * contract; bounded by node-pty's five-second connection bound plus a
379
+ * margin). Until then the session is starting: it holds a capacity slot but
380
+ * is not listed and not in the dashboard feed, so nobody sees a session
381
+ * that may never exist. A session that ends before any program did fails
382
+ * the start with the status and shape 1.1.0's thrown spawn had (500,
383
+ * "Failed to start session: Error: ..."), is ended and discarded, and null
384
+ * is returned with the response already sent. Only the proven ConPTY worker
385
+ * timeout gets one replacement attempt under the same id.
386
+ */
387
+ private programStarted;
388
+ /** The sessions a client or the dashboard may see: every one but those still starting. */
389
+ private listedSummaries;
390
+ /**
391
+ * Session list decorated with whatever prompt each session is holding. The
392
+ * stall alert also goes to `daemon.log` and the `/events` socket, but an
393
+ * agent reaches the daemon through request-and-answer tool calls or one-shot
394
+ * CLI runs, so it can never hold that socket: `terminal_list` is how a
395
+ * manager agent learns that its delegate is blocked, which is the whole
396
+ * point of detecting it.
397
+ *
398
+ * Read from the scanner's own state rather than re-detecting here: one
399
+ * source of truth means what the list reports and what the log announces
400
+ * cannot disagree, and a list call stays cheap (no render per session).
401
+ * The cost is staleness bounded by the scan interval: 5 s, immaterial
402
+ * against prompts that sit for minutes.
403
+ */
404
+ private listWithPrompts;
405
+ private summaryWithPrompt;
406
+ private updateFeedSession;
407
+ private clearPromptGraceCheck;
408
+ private schedulePromptGraceCheck;
409
+ /**
410
+ * Scan every live session's screen for a permission prompt and alert when
411
+ * the SAME prompt has been waiting longer than the stall threshold, so an
412
+ * unserviced prompt does not cost the delegate tens of idle minutes until
413
+ * someone happens to snapshot the session.
414
+ */
415
+ private scanForPromptStalls;
416
+ /** Fan an event out to every `/events` subscriber (TEXT JSON frames). */
417
+ private emitEvent;
418
+ private handleUpgrade;
419
+ /** Attach a subscriber to the daemon-wide event stream. */
420
+ private attachEventSocket;
421
+ /**
422
+ * Attach a subscriber to the dashboard session feed. Frames are the feed's
423
+ * own: hello, a snapshot, then session deltas. The only inbound frame read is
424
+ * one small window registration; every other inbound frame is ignored.
425
+ * The socket counts as connected so idle shutdown waits while the dashboard
426
+ * is open.
427
+ */
428
+ private attachDashboardSocket;
429
+ private hasLiveDashboardSocket;
430
+ /** Send a JSON control message (TEXT frame) to every watcher of a session. */
431
+ private broadcastControl;
432
+ /**
433
+ * Tell every watcher the current grid and who drives it, then hand each one
434
+ * a snapshot at that size. Both halves matter: the frame moves their grid,
435
+ * and the snapshot repaints it from the daemon's renderer rather than from
436
+ * xterm's in-place reflow of what they had (§5).
437
+ */
438
+ private announceGeometry;
439
+ /** What one watcher is told about the grid, from its own point of view. */
440
+ private geometryFrame;
441
+ /** The viewer URL for a session, carrying a one-use grant. */
442
+ private viewerUrlFor;
443
+ /** The daemon's rendered state as viewer bytes: a wipe, then the rebuild. */
444
+ private snapshotForViewer;
445
+ /**
446
+ * Wire protocol: BINARY frames carry terminal bytes (snapshots, live mirror,
447
+ * exit notice); TEXT frames are JSON control messages. Incoming text frames
448
+ * are either JSON controls (resize) or bytes for the program: a person's
449
+ * keystrokes, or the viewer's terminal answering the program
450
+ * (terminalReports.ts).
451
+ */
452
+ private attachSocket;
453
+ }