shellpuppet 0.1.0 → 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 (95) hide show
  1. package/README.md +191 -32
  2. package/dist/cli/desktopShortcut.d.ts +22 -0
  3. package/dist/cli/desktopShortcut.js +157 -0
  4. package/dist/cli/desktopShortcut.js.map +1 -0
  5. package/dist/cli/index.js +205 -52
  6. package/dist/cli/index.js.map +1 -1
  7. package/dist/client/http.d.ts +62 -1
  8. package/dist/client/http.js +388 -32
  9. package/dist/client/http.js.map +1 -1
  10. package/dist/core/dialogs.d.ts +20 -12
  11. package/dist/core/dialogs.js +31 -19
  12. package/dist/core/dialogs.js.map +1 -1
  13. package/dist/core/manager.d.ts +35 -5
  14. package/dist/core/manager.js +121 -8
  15. package/dist/core/manager.js.map +1 -1
  16. package/dist/core/nativePty.d.ts +9 -5
  17. package/dist/core/nativePty.js +11 -6
  18. package/dist/core/nativePty.js.map +1 -1
  19. package/dist/core/promptDetector.d.ts +37 -3
  20. package/dist/core/promptDetector.js +303 -12
  21. package/dist/core/promptDetector.js.map +1 -1
  22. package/dist/core/reasoning.d.ts +19 -6
  23. package/dist/core/reasoning.js +223 -30
  24. package/dist/core/reasoning.js.map +1 -1
  25. package/dist/core/regexBudget.d.ts +20 -0
  26. package/dist/core/regexBudget.js +25 -1
  27. package/dist/core/regexBudget.js.map +1 -1
  28. package/dist/core/renderer.d.ts +16 -1
  29. package/dist/core/renderer.js +97 -6
  30. package/dist/core/renderer.js.map +1 -1
  31. package/dist/core/selection.d.ts +6 -0
  32. package/dist/core/selection.js +48 -0
  33. package/dist/core/selection.js.map +1 -0
  34. package/dist/core/session.d.ts +266 -12
  35. package/dist/core/session.js +583 -99
  36. package/dist/core/session.js.map +1 -1
  37. package/dist/core/types.d.ts +53 -18
  38. package/dist/core/types.js +30 -6
  39. package/dist/core/types.js.map +1 -1
  40. package/dist/core/waiter.d.ts +12 -4
  41. package/dist/core/waiter.js +63 -70
  42. package/dist/core/waiter.js.map +1 -1
  43. package/dist/core/windowsConsoleHost.d.ts +147 -0
  44. package/dist/core/windowsConsoleHost.js +588 -0
  45. package/dist/core/windowsConsoleHost.js.map +1 -0
  46. package/dist/core/windowsPtyStart.d.ts +100 -29
  47. package/dist/core/windowsPtyStart.js +143 -36
  48. package/dist/core/windowsPtyStart.js.map +1 -1
  49. package/dist/daemon/auth.d.ts +72 -2
  50. package/dist/daemon/auth.js +128 -3
  51. package/dist/daemon/auth.js.map +1 -1
  52. package/dist/daemon/config.d.ts +44 -1
  53. package/dist/daemon/config.js +276 -9
  54. package/dist/daemon/config.js.map +1 -1
  55. package/dist/daemon/folderPicker.d.ts +27 -0
  56. package/dist/daemon/folderPicker.js +115 -0
  57. package/dist/daemon/folderPicker.js.map +1 -0
  58. package/dist/daemon/index.js +20 -19
  59. package/dist/daemon/index.js.map +1 -1
  60. package/dist/daemon/launchProfiles.d.ts +155 -0
  61. package/dist/daemon/launchProfiles.js +554 -0
  62. package/dist/daemon/launchProfiles.js.map +1 -0
  63. package/dist/daemon/paths.d.ts +2 -0
  64. package/dist/daemon/paths.js.map +1 -1
  65. package/dist/daemon/screenshot.d.ts +58 -1
  66. package/dist/daemon/screenshot.js +172 -26
  67. package/dist/daemon/screenshot.js.map +1 -1
  68. package/dist/daemon/server.d.ts +219 -10
  69. package/dist/daemon/server.js +1970 -203
  70. package/dist/daemon/server.js.map +1 -1
  71. package/dist/daemon/sessionFeed.d.ts +48 -0
  72. package/dist/daemon/sessionFeed.js +101 -0
  73. package/dist/daemon/sessionFeed.js.map +1 -0
  74. package/dist/daemon/viewerLauncher.d.ts +100 -9
  75. package/dist/daemon/viewerLauncher.js +349 -67
  76. package/dist/daemon/viewerLauncher.js.map +1 -1
  77. package/dist/mcp/config.d.ts +7 -14
  78. package/dist/mcp/config.js +10 -21
  79. package/dist/mcp/config.js.map +1 -1
  80. package/dist/mcp/index.js +20 -11
  81. package/dist/mcp/index.js.map +1 -1
  82. package/dist/mcp/tools.d.ts +14 -7
  83. package/dist/mcp/tools.js +161 -60
  84. package/dist/mcp/tools.js.map +1 -1
  85. package/dist/viewer/dashboard-folders.js +20 -0
  86. package/dist/viewer/dashboard-layout.js +52 -0
  87. package/dist/viewer/dashboard.css +67 -0
  88. package/dist/viewer/dashboard.html +44 -0
  89. package/dist/viewer/dashboard.js +1518 -0
  90. package/dist/viewer/icon.ico +0 -0
  91. package/dist/viewer/index.html +12 -7
  92. package/dist/viewer/pane.js +736 -0
  93. package/dist/viewer/viewer.css +18 -7
  94. package/dist/viewer/viewer.js +47 -624
  95. package/package.json +13 -13
@@ -5,15 +5,19 @@
5
5
  * Host-header validation on every request, Origin validation on WebSocket
6
6
  * upgrades, bearer-token auth on the API, no CORS headers at all.
7
7
  */
8
- import type { Logger, PolicyConfig, SessionEnvMode } from '../core/types.js';
8
+ import type { Session } from '../core/session.js';
9
+ import type { Logger, PolicyConfig, SessionOptions, SessionEnvMode } from '../core/types.js';
9
10
  import { launchViewer } from './viewerLauncher.js';
10
11
  import { captureViewer } from './screenshot.js';
11
12
  import type { ViewerLaunchConfig } from './viewerLauncher.js';
12
13
  import type { Auditor } from './audit.js';
13
14
  import type { DaemonState } from './paths.js';
15
+ import type { FeedFrame } from './sessionFeed.js';
16
+ import type { LauncherConfig } from './launchProfiles.js';
14
17
  export interface DaemonOptions {
15
18
  home: string;
16
19
  logger: Logger;
20
+ startedWrapped?: boolean;
17
21
  policy?: PolicyConfig;
18
22
  /** Session environment hygiene (design §4.6, ADR-008). Defaults to clean. */
19
23
  sessionEnv?: SessionEnvMode;
@@ -33,11 +37,16 @@ export interface DaemonOptions {
33
37
  promptStallMs?: number;
34
38
  /**
35
39
  * How the viewer window is opened on session start. Absent means
36
- * "none", a deliberately inert default, so a test daemon cannot put browser
40
+ * "no-window", a deliberately inert default, so a test daemon cannot put browser
37
41
  * windows on someone's desktop by forgetting to opt out. The daemon entry
38
- * point supplies the product default of `isolated-app`.
42
+ * point supplies the product default of `dashboard`.
39
43
  */
40
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;
41
50
  /**
42
51
  * Test seam: what actually opens the window. Defaults to the real launcher.
43
52
  * It exists so a test can see the URL the daemon would open, and check
@@ -50,8 +59,26 @@ export interface DaemonOptions {
50
59
  * without starting a browser, and check that it may watch but never write.
51
60
  */
52
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;
53
81
  }
54
- /** Thrown by `start()` when a healthy daemon already owns this home. */
55
82
  export declare class AlreadyRunningError extends Error {
56
83
  readonly state: DaemonState;
57
84
  constructor(state: DaemonState);
@@ -66,25 +93,45 @@ export declare class Daemon {
66
93
  private readonly sockets;
67
94
  /** Every watcher of a session, with the gate that orders its stream across snapshots. */
68
95
  private readonly sessionSockets;
96
+ /** Frozen replay bytes belong only to the screenshot's one-use viewer grant. */
97
+ private readonly captureSnapshots;
69
98
  /**
70
99
  * The viewport currently driving each session's grid (§5).
71
100
  *
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.
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.
78
109
  */
79
110
  private readonly geometryController;
111
+ /** A dashboard document keeps its grid while a covered pane reconnects. */
112
+ private readonly dashboardGeometryOwner;
80
113
  /** Subscribers to the daemon-wide event stream (`/events`). */
81
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;
82
128
  /**
83
129
  * Per-session prompt tracking for stall alerts: the signature of the prompt
84
130
  * currently on screen, when it was first seen and whether it has alerted.
85
131
  */
86
132
  private readonly promptStates;
87
133
  private readonly version;
134
+ private readonly sessionFeed;
88
135
  private portValue;
89
136
  private waitsInFlight;
90
137
  private lastActivity;
@@ -97,6 +144,7 @@ export declare class Daemon {
97
144
  /** The pid of another running daemon already reported as owning daemon.json. */
98
145
  private foreignOwnerReported;
99
146
  private promptScanTimer;
147
+ private readonly promptGraceTimers;
100
148
  private promptScanRunning;
101
149
  private stopPromise;
102
150
  private readonly audit;
@@ -105,8 +153,27 @@ export declare class Daemon {
105
153
  * that what the daemon reports is what its sessions get.
106
154
  */
107
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;
108
173
  constructor(options: DaemonOptions);
109
174
  get port(): number;
175
+ /** Clock for dashboard grant expiry. Tests inject one; otherwise wall time. */
176
+ private now;
110
177
  get token(): string;
111
178
  get connectedSockets(): number;
112
179
  /**
@@ -116,6 +183,8 @@ export declare class Daemon {
116
183
  */
117
184
  get waitsPolling(): number;
118
185
  get liveSessions(): number;
186
+ /** Subscribe inside the daemon to the dashboard's session feed. */
187
+ subscribeDashboardFeed(listener: (frame: FeedFrame) => void): () => void;
119
188
  start(): Promise<{
120
189
  port: number;
121
190
  token: string;
@@ -157,6 +226,119 @@ export declare class Daemon {
157
226
  private environmentReport;
158
227
  private hostAllowed;
159
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;
160
342
  private handleRequest;
161
343
  private handleStart;
162
344
  /**
@@ -191,6 +373,20 @@ export declare class Daemon {
191
373
  private dropSessionArtifacts;
192
374
  /** Whether a live viewport is currently driving this session's grid. */
193
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;
194
390
  /**
195
391
  * Session list decorated with whatever prompt each session is holding. The
196
392
  * stall alert also goes to `daemon.log` and the `/events` socket, but an
@@ -206,6 +402,10 @@ export declare class Daemon {
206
402
  * against prompts that sit for minutes.
207
403
  */
208
404
  private listWithPrompts;
405
+ private summaryWithPrompt;
406
+ private updateFeedSession;
407
+ private clearPromptGraceCheck;
408
+ private schedulePromptGraceCheck;
209
409
  /**
210
410
  * Scan every live session's screen for a permission prompt and alert when
211
411
  * the SAME prompt has been waiting longer than the stall threshold, so an
@@ -218,6 +418,15 @@ export declare class Daemon {
218
418
  private handleUpgrade;
219
419
  /** Attach a subscriber to the daemon-wide event stream. */
220
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;
221
430
  /** Send a JSON control message (TEXT frame) to every watcher of a session. */
222
431
  private broadcastControl;
223
432
  /**