shellpuppet 0.1.0 → 0.2.1

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 (101) hide show
  1. package/README.md +210 -33
  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 +268 -59
  6. package/dist/cli/index.js.map +1 -1
  7. package/dist/client/http.d.ts +74 -3
  8. package/dist/client/http.js +560 -37
  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 +267 -13
  35. package/dist/core/session.js +585 -101
  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/liveness.d.ts +1 -1
  64. package/dist/daemon/liveness.js +2 -2
  65. package/dist/daemon/liveness.js.map +1 -1
  66. package/dist/daemon/paths.d.ts +4 -0
  67. package/dist/daemon/paths.js.map +1 -1
  68. package/dist/daemon/restartHold.d.ts +16 -0
  69. package/dist/daemon/restartHold.js +126 -0
  70. package/dist/daemon/restartHold.js.map +1 -0
  71. package/dist/daemon/screenshot.d.ts +58 -1
  72. package/dist/daemon/screenshot.js +172 -26
  73. package/dist/daemon/screenshot.js.map +1 -1
  74. package/dist/daemon/server.d.ts +220 -10
  75. package/dist/daemon/server.js +1975 -205
  76. package/dist/daemon/server.js.map +1 -1
  77. package/dist/daemon/sessionFeed.d.ts +48 -0
  78. package/dist/daemon/sessionFeed.js +101 -0
  79. package/dist/daemon/sessionFeed.js.map +1 -0
  80. package/dist/daemon/viewerLauncher.d.ts +100 -9
  81. package/dist/daemon/viewerLauncher.js +349 -67
  82. package/dist/daemon/viewerLauncher.js.map +1 -1
  83. package/dist/mcp/config.d.ts +7 -14
  84. package/dist/mcp/config.js +10 -21
  85. package/dist/mcp/config.js.map +1 -1
  86. package/dist/mcp/index.js +20 -11
  87. package/dist/mcp/index.js.map +1 -1
  88. package/dist/mcp/tools.d.ts +14 -7
  89. package/dist/mcp/tools.js +161 -60
  90. package/dist/mcp/tools.js.map +1 -1
  91. package/dist/viewer/dashboard-folders.js +20 -0
  92. package/dist/viewer/dashboard-layout.js +52 -0
  93. package/dist/viewer/dashboard.css +67 -0
  94. package/dist/viewer/dashboard.html +44 -0
  95. package/dist/viewer/dashboard.js +1523 -0
  96. package/dist/viewer/icon.ico +0 -0
  97. package/dist/viewer/index.html +12 -7
  98. package/dist/viewer/pane.js +736 -0
  99. package/dist/viewer/viewer.css +18 -7
  100. package/dist/viewer/viewer.js +47 -624
  101. 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;
@@ -94,9 +141,11 @@ export declare class Daemon {
94
141
  private statePublishRunning;
95
142
  /** What this daemon published in daemon.json, once it has claimed it. */
96
143
  private publishedState;
144
+ private readonly instanceId;
97
145
  /** The pid of another running daemon already reported as owning daemon.json. */
98
146
  private foreignOwnerReported;
99
147
  private promptScanTimer;
148
+ private readonly promptGraceTimers;
100
149
  private promptScanRunning;
101
150
  private stopPromise;
102
151
  private readonly audit;
@@ -105,8 +154,27 @@ export declare class Daemon {
105
154
  * that what the daemon reports is what its sessions get.
106
155
  */
107
156
  private readonly sessionEnv;
157
+ /** The profiles the dashboard may name, and the entries that did not load. */
158
+ private readonly launchProfiles;
159
+ private readonly daemonEnv;
160
+ /** The global policy, kept here so a launch can report it before creating. */
161
+ private readonly compiledPolicy;
162
+ /** Accepted launch times, for the sliding sixty-second cap. */
163
+ private readonly launchTimes;
164
+ private folderPickerBusy;
165
+ private folderPickerAbort;
166
+ private folderPickerDone;
167
+ /** Includes requests still reading their body when shutdown begins. */
168
+ private readonly folderPickerHandlers;
169
+ /**
170
+ * Sessions created whose program does not exist yet (`programStarted`):
171
+ * not listed and not in the feed until the start contract is met.
172
+ */
173
+ private readonly startingIds;
108
174
  constructor(options: DaemonOptions);
109
175
  get port(): number;
176
+ /** Clock for dashboard grant expiry. Tests inject one; otherwise wall time. */
177
+ private now;
110
178
  get token(): string;
111
179
  get connectedSockets(): number;
112
180
  /**
@@ -116,6 +184,8 @@ export declare class Daemon {
116
184
  */
117
185
  get waitsPolling(): number;
118
186
  get liveSessions(): number;
187
+ /** Subscribe inside the daemon to the dashboard's session feed. */
188
+ subscribeDashboardFeed(listener: (frame: FeedFrame) => void): () => void;
119
189
  start(): Promise<{
120
190
  port: number;
121
191
  token: string;
@@ -157,6 +227,119 @@ export declare class Daemon {
157
227
  private environmentReport;
158
228
  private hostAllowed;
159
229
  private originAllowed;
230
+ /**
231
+ * An Origin that is this daemon's own and whose host matches `Host`.
232
+ * `127.0.0.1` and `localhost` are both allowed names, and they are not the
233
+ * same host: a cookie minted for one must not be presented as the other.
234
+ */
235
+ private originMatchesRequest;
236
+ /** Top-level document load of the dashboard. Origin may be absent. */
237
+ private isDocumentNavigation;
238
+ /** Same-origin read, such as `GET /sessions` from the dashboard page. */
239
+ private isSameOriginRead;
240
+ /** `same-site`, `cross-site`, or any site value that is not a dashboard site. */
241
+ private forbiddenFetchSite;
242
+ private dashboardCookiePresented;
243
+ /** The first `sp_dash` value the daemon issued for the current credential. */
244
+ private dashboardCookieToken;
245
+ private auditDashboardRefusal;
246
+ /**
247
+ * Why a presented cookie was refused on a route that may omit Origin:
248
+ * the dashboard page, outside a document navigation, and `GET /sessions`.
249
+ * A present Origin that does not match is `origin`. These routes allow an
250
+ * absent Origin, so that absence is not the reason: the Fetch Metadata is.
251
+ */
252
+ private cookieRefusalReason;
253
+ private dashboardDocument;
254
+ private sendDashboardPage;
255
+ /**
256
+ * `GET` and `HEAD /dashboard`. A grant is spent only by an accepted
257
+ * document navigation, which redirects with a cookie before any page loads.
258
+ * A fetch does not spend it. A bad, spent or absent grant without a live
259
+ * cookie returns the guidance page without revealing whether it was real.
260
+ */
261
+ private handleDashboardPage;
262
+ private openDashboardWindow;
263
+ private handleDashboardOpen;
264
+ private handleDesktopOpen;
265
+ private openDashboardForStart;
266
+ private writeSessionList;
267
+ private waitingSessions;
268
+ private waitAcrossSessions;
269
+ /** Cookie-authenticated `GET /sessions`. A missing or wrong proof is a 403. */
270
+ private serveDashboardSessionList;
271
+ /** A cookie mutation: same-origin Fetch Metadata and a matching Origin. */
272
+ private isSameOriginMutation;
273
+ /**
274
+ * Why a cookie mutation is refused, for the audit. A missing or foreign
275
+ * `Origin` is `origin`; a request whose `Origin` is this page's but whose
276
+ * `Sec-Fetch-Site` is not `same-origin` is `fetch-site`. The route needs
277
+ * both, so this only decides the reason a request failing both is recorded
278
+ * under: naming the Origin keeps the two defences apart in the log.
279
+ */
280
+ private mutationRefusalReason;
281
+ /**
282
+ * What the page needs to show a profile, and nothing secret: the command
283
+ * and arguments are the profile's own, and `envFromDaemon` contributes
284
+ * configured names or "all". `{folder}` stays unexpanded here because it
285
+ * depends on the click.
286
+ */
287
+ private profileDescription;
288
+ /** Cookie-authenticated `GET /dashboard/profiles`. A missing proof is a 403. */
289
+ private serveDashboardProfiles;
290
+ /** The desktop picker is available only to an authenticated any-folder profile. */
291
+ private handleDashboardFolderPicker;
292
+ /**
293
+ * The directory a launch starts in: the selected folder's configured path,
294
+ * the profile's fixed path, or the user's home. Relative paths are taken
295
+ * from the user's home, as a direct `POST /sessions` does. The path is a
296
+ * spawn `cwd`, never a command-line fragment.
297
+ */
298
+ private resolveLaunchCwd;
299
+ /** Accepted launch times inside the last sixty seconds, oldest first. */
300
+ private pruneLaunchTimes;
301
+ /**
302
+ * `POST /dashboard/launch`: one configured profile, and nothing else. The
303
+ * body names a profile and, when that profile asks, a folder. Only profiles
304
+ * configured for any folder may receive a path. The page cannot supply a
305
+ * command, arguments or an environment value.
306
+ */
307
+ private handleDashboardLaunch;
308
+ /**
309
+ * `DELETE /sessions/<id>` for the dashboard cookie. Origin, Fetch Metadata
310
+ * and the document proof are the same checks as launch. One call kills the
311
+ * session and audits actor `dashboard`. A client token never reaches here.
312
+ */
313
+ private handleDashboardKill;
314
+ /**
315
+ * Why a `/dashboard/events` upgrade is refused, or null when it may attach.
316
+ * The client token on the query is not a way in. The proof is the
317
+ * subprotocol, not `X-ShellPuppet-CSRF`.
318
+ */
319
+ private dashboardUpgradeRefusal;
320
+ /**
321
+ * Select `sp-csrf.<proof>` when this upgrade's credential is the dashboard
322
+ * cookie: the dashboard feed, or a session stream the client token did not
323
+ * take. The echo names that credential. A viewer or client socket, including
324
+ * a client token that also carries the cookie and offers a live proof,
325
+ * selects none. Returning false leaves the upgrade with none selected.
326
+ */
327
+ private dashboardProtocol;
328
+ /**
329
+ * Who may open `/sessions/<id>/stream`, or why not.
330
+ *
331
+ * A client token and a viewer cookie for this session keep the check they
332
+ * already have: a missing Origin is allowed, and a foreign one is refused.
333
+ * The dashboard cookie is a third way in, and only with the events
334
+ * upgrade's rules: a live `sp-csrf` subprotocol, a matching Origin, and
335
+ * same-origin Fetch Metadata. A header is not the proof. A viewer cookie,
336
+ * including a read-only one, is never treated as the dashboard credential.
337
+ * When both are present and the proof is live, the dashboard cookie is the
338
+ * one that opens the socket, so a read-only viewer cookie in the same jar
339
+ * does not mask it. When the proof is missing, a viewer cookie for this
340
+ * session still opens the socket on its own rules.
341
+ */
342
+ private sessionStreamDecision;
160
343
  private handleRequest;
161
344
  private handleStart;
162
345
  /**
@@ -191,6 +374,20 @@ export declare class Daemon {
191
374
  private dropSessionArtifacts;
192
375
  /** Whether a live viewport is currently driving this session's grid. */
193
376
  private hasGeometryController;
377
+ /**
378
+ * Wait, for a session just created, until its program exists (the start
379
+ * contract; bounded by node-pty's five-second connection bound plus a
380
+ * margin). Until then the session is starting: it holds a capacity slot but
381
+ * is not listed and not in the dashboard feed, so nobody sees a session
382
+ * that may never exist. A session that ends before any program did fails
383
+ * the start with the status and shape 1.1.0's thrown spawn had (500,
384
+ * "Failed to start session: Error: ..."), is ended and discarded, and null
385
+ * is returned with the response already sent. Only the proven ConPTY worker
386
+ * timeout gets one replacement attempt under the same id.
387
+ */
388
+ private programStarted;
389
+ /** The sessions a client or the dashboard may see: every one but those still starting. */
390
+ private listedSummaries;
194
391
  /**
195
392
  * Session list decorated with whatever prompt each session is holding. The
196
393
  * stall alert also goes to `daemon.log` and the `/events` socket, but an
@@ -206,6 +403,10 @@ export declare class Daemon {
206
403
  * against prompts that sit for minutes.
207
404
  */
208
405
  private listWithPrompts;
406
+ private summaryWithPrompt;
407
+ private updateFeedSession;
408
+ private clearPromptGraceCheck;
409
+ private schedulePromptGraceCheck;
209
410
  /**
210
411
  * Scan every live session's screen for a permission prompt and alert when
211
412
  * the SAME prompt has been waiting longer than the stall threshold, so an
@@ -218,6 +419,15 @@ export declare class Daemon {
218
419
  private handleUpgrade;
219
420
  /** Attach a subscriber to the daemon-wide event stream. */
220
421
  private attachEventSocket;
422
+ /**
423
+ * Attach a subscriber to the dashboard session feed. Frames are the feed's
424
+ * own: hello, a snapshot, then session deltas. The only inbound frame read is
425
+ * one small window registration; every other inbound frame is ignored.
426
+ * The socket counts as connected so idle shutdown waits while the dashboard
427
+ * is open.
428
+ */
429
+ private attachDashboardSocket;
430
+ private hasLiveDashboardSocket;
221
431
  /** Send a JSON control message (TEXT frame) to every watcher of a session. */
222
432
  private broadcastControl;
223
433
  /**