shellpuppet 0.5.0 → 0.5.2
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.
- package/README.md +1 -1
- package/dist/agents/claude/nudge.js +1 -1
- package/dist/agents/claude/nudge.js.map +1 -1
- package/dist/agents/index.d.ts +2 -0
- package/dist/agents/index.js +7 -0
- package/dist/agents/index.js.map +1 -1
- package/dist/cli/index.js +23 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/core/mailbox.d.ts +17 -0
- package/dist/core/mailbox.js +14 -0
- package/dist/core/mailbox.js.map +1 -1
- package/dist/core/renderer.d.ts +4 -0
- package/dist/core/renderer.js +30 -2
- package/dist/core/renderer.js.map +1 -1
- package/dist/core/session.d.ts +26 -2
- package/dist/core/session.js +119 -20
- package/dist/core/session.js.map +1 -1
- package/dist/daemon/launchProfiles.d.ts +3 -2
- package/dist/daemon/launchProfiles.js +6 -4
- package/dist/daemon/launchProfiles.js.map +1 -1
- package/dist/daemon/screenshot.d.ts +2 -0
- package/dist/daemon/screenshot.js +21 -3
- package/dist/daemon/screenshot.js.map +1 -1
- package/dist/daemon/server.d.ts +6 -1
- package/dist/daemon/server.js +377 -254
- package/dist/daemon/server.js.map +1 -1
- package/dist/daemon/terminalReports.d.ts +9 -1
- package/dist/daemon/terminalReports.js +54 -0
- package/dist/daemon/terminalReports.js.map +1 -1
- package/dist/viewer/dashboard.css +3 -0
- package/dist/viewer/dashboard.html +1 -1
- package/dist/viewer/dashboard.js +83 -5
- package/dist/viewer/pane.js +10 -6
- package/package.json +1 -1
package/dist/daemon/server.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
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 { moduleFor } from '../agents/index.js';
|
|
8
|
+
import { hasAgentShellNoColor, moduleFor } from '../agents/index.js';
|
|
9
9
|
import * as fs from 'node:fs';
|
|
10
10
|
import { randomUUID } from 'node:crypto';
|
|
11
11
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
@@ -41,7 +41,7 @@ import { acquireHomeOwner, AlreadyRunningError } from './homeOwner.js';
|
|
|
41
41
|
import { daemonRunning } from './liveness.js';
|
|
42
42
|
import { restoreRestartSource, retryRestartSource, snapshotSourceId } from './restartSource.js';
|
|
43
43
|
import { packageVersion } from './meta.js';
|
|
44
|
-
import {
|
|
44
|
+
import { terminalReportBatchKind } from './terminalReports.js';
|
|
45
45
|
import { ViewerSync } from './viewerSync.js';
|
|
46
46
|
import { SessionFeed } from './sessionFeed.js';
|
|
47
47
|
import { compileProfilePolicy, copyDaemonEnv, describeProfileText, expandProfileText, resolveLaunchCommand, resolveAnyFolder, } from './launchProfiles.js';
|
|
@@ -454,6 +454,7 @@ function startShapeError(body) {
|
|
|
454
454
|
}
|
|
455
455
|
return null;
|
|
456
456
|
}
|
|
457
|
+
const QUEUED_NUDGE_MAX_MS = 5 * 60_000;
|
|
457
458
|
export class Daemon {
|
|
458
459
|
options;
|
|
459
460
|
logger;
|
|
@@ -461,6 +462,8 @@ export class Daemon {
|
|
|
461
462
|
manager;
|
|
462
463
|
mailbox;
|
|
463
464
|
nudges;
|
|
465
|
+
queuedMessageNudges = new Map();
|
|
466
|
+
inboxReadVersions = new Map();
|
|
464
467
|
schedules = new ScheduleStore();
|
|
465
468
|
pendingSchedules = new Map();
|
|
466
469
|
promptScheduler;
|
|
@@ -564,6 +567,7 @@ export class Daemon {
|
|
|
564
567
|
launchProfiles;
|
|
565
568
|
launchesPerMinute;
|
|
566
569
|
daemonEnv;
|
|
570
|
+
omitInheritedNoColor;
|
|
567
571
|
cliPath;
|
|
568
572
|
/** The global policy, kept here so a launch can report it before creating. */
|
|
569
573
|
compiledPolicy;
|
|
@@ -601,11 +605,13 @@ export class Daemon {
|
|
|
601
605
|
this.sessionEnv = options.sessionEnv ?? DEFAULT_SESSION_ENV;
|
|
602
606
|
this.launchProfiles = options.launchProfiles ?? { profiles: [], problems: [] };
|
|
603
607
|
this.launchesPerMinute = this.launchProfiles.launchesPerMinute ?? DEFAULT_LAUNCHES_PER_MINUTE;
|
|
604
|
-
this.daemonEnv = options.daemonEnv ?? process.env;
|
|
608
|
+
this.daemonEnv = { ...(options.daemonEnv ?? process.env) };
|
|
609
|
+
this.omitInheritedNoColor = hasAgentShellNoColor(this.daemonEnv);
|
|
605
610
|
this.cliPath = fileURLToPath(new URL('../../dist/cli/index.js', import.meta.url));
|
|
606
611
|
this.compiledPolicy = compilePolicy(options.policy);
|
|
607
612
|
this.manager = new SessionManager(compilePolicy(options.policy), this.logger, {
|
|
608
613
|
sessionEnv: this.sessionEnv,
|
|
614
|
+
omitInheritedNoColor: this.omitInheritedNoColor,
|
|
609
615
|
identity: {
|
|
610
616
|
token: '',
|
|
611
617
|
instanceId: this.instanceId,
|
|
@@ -645,6 +651,11 @@ export class Daemon {
|
|
|
645
651
|
});
|
|
646
652
|
this.mailbox.on('state', (event) => {
|
|
647
653
|
const entry = this.mailbox.log().find((item) => item.id === event.id);
|
|
654
|
+
if (event.state === 'read' && entry !== undefined) {
|
|
655
|
+
const id = entry.to.id;
|
|
656
|
+
this.inboxReadVersions.set(id, (this.inboxReadVersions.get(id) ?? 0) + 1);
|
|
657
|
+
this.queuedMessageNudges.delete(id);
|
|
658
|
+
}
|
|
648
659
|
this.emitMessageEvent({ type: 'message-state', ...event }, entry);
|
|
649
660
|
this.emitDashboardMessageEvent({ type: 'message-state', ...event });
|
|
650
661
|
});
|
|
@@ -827,6 +838,8 @@ export class Daemon {
|
|
|
827
838
|
// Said once, at start, because passthrough leaks silently otherwise
|
|
828
839
|
// (ADR-008): the names only, never the values.
|
|
829
840
|
const { exposed } = this.environmentReport();
|
|
841
|
+
if (this.omitInheritedNoColor)
|
|
842
|
+
this.logger.info('omitting inherited output setting from agent tool shell', { names: ['NO_COLOR'] });
|
|
830
843
|
if (exposed.length > 0) {
|
|
831
844
|
this.logger.warn('sessionEnv "passthrough" hands every session the daemon environment, including ' +
|
|
832
845
|
'these variables whose names look like credentials; use "clean" or a list to keep them out', { names: exposed });
|
|
@@ -1520,8 +1533,16 @@ export class Daemon {
|
|
|
1520
1533
|
this.lastActivity = Date.now();
|
|
1521
1534
|
this.writeSessionList(res, url);
|
|
1522
1535
|
}
|
|
1536
|
+
/** Build the metadata response without creating a text-bearing mailbox entry. */
|
|
1537
|
+
writeUnreadAge(res, actor) {
|
|
1538
|
+
const messages = this.mailbox.unreadAge(this.now());
|
|
1539
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
1540
|
+
this.audit.record(actor, 'unread-age-read', undefined, { count: messages.length });
|
|
1541
|
+
this.lastActivity = Date.now();
|
|
1542
|
+
json(res, 200, { messages });
|
|
1543
|
+
}
|
|
1523
1544
|
/** Owner-wide history is available only with the dashboard cookie and document proof. */
|
|
1524
|
-
serveDashboardMessages(req, res, url) {
|
|
1545
|
+
serveDashboardMessages(req, res, url, unreadAge = false) {
|
|
1525
1546
|
const presented = this.dashboardCookiePresented(req.headers.cookie);
|
|
1526
1547
|
if (this.dashboardCookieToken(req.headers.cookie) === null) {
|
|
1527
1548
|
if (presented)
|
|
@@ -1539,6 +1560,10 @@ export class Daemon {
|
|
|
1539
1560
|
json(res, 403, { error: 'Forbidden.' });
|
|
1540
1561
|
return;
|
|
1541
1562
|
}
|
|
1563
|
+
if (unreadAge) {
|
|
1564
|
+
this.writeUnreadAge(res, 'dashboard');
|
|
1565
|
+
return;
|
|
1566
|
+
}
|
|
1542
1567
|
const messages = this.mailbox.log();
|
|
1543
1568
|
const counts = {};
|
|
1544
1569
|
for (const entry of messages) {
|
|
@@ -1947,7 +1972,7 @@ export class Daemon {
|
|
|
1947
1972
|
});
|
|
1948
1973
|
return;
|
|
1949
1974
|
}
|
|
1950
|
-
const { env, copied } = copyDaemonEnv(profile.envFromDaemon, this.daemonEnv);
|
|
1975
|
+
const { env, copied } = copyDaemonEnv(profile.envFromDaemon, this.daemonEnv, undefined, this.omitInheritedNoColor);
|
|
1951
1976
|
const envNames = Object.keys(env);
|
|
1952
1977
|
const folderLabel = freeCwd !== undefined ? freeCwd : folder?.name ?? (rootCwd !== undefined ? folderName : undefined);
|
|
1953
1978
|
const options = {
|
|
@@ -2428,6 +2453,10 @@ export class Daemon {
|
|
|
2428
2453
|
this.serveDashboardMessages(req, res, url);
|
|
2429
2454
|
return;
|
|
2430
2455
|
}
|
|
2456
|
+
if (req.method === 'GET' && url.pathname === '/dashboard/messages/unread-age') {
|
|
2457
|
+
this.serveDashboardMessages(req, res, url, true);
|
|
2458
|
+
return;
|
|
2459
|
+
}
|
|
2431
2460
|
if (req.method === 'POST' && url.pathname === '/dashboard/launch') {
|
|
2432
2461
|
await this.handleDashboardLaunch(req, res);
|
|
2433
2462
|
return;
|
|
@@ -2630,6 +2659,8 @@ export class Daemon {
|
|
|
2630
2659
|
if (pendingId === id)
|
|
2631
2660
|
this.pendingSchedules.delete(name);
|
|
2632
2661
|
this.nudges.remove(id);
|
|
2662
|
+
this.queuedMessageNudges.delete(id);
|
|
2663
|
+
this.inboxReadVersions.delete(id);
|
|
2633
2664
|
// Audit before end can trim old tombstones from retained history.
|
|
2634
2665
|
for (const entry of this.mailbox.log({ participantId: id })) {
|
|
2635
2666
|
if (entry.to.id === id && (entry.state === 'waiting' || entry.state === 'nudged')) {
|
|
@@ -2654,38 +2685,68 @@ export class Daemon {
|
|
|
2654
2685
|
}
|
|
2655
2686
|
async deliverNudge(id) {
|
|
2656
2687
|
const session = this.manager.get(id);
|
|
2657
|
-
if (session === undefined || session.exited || this.mailbox.participant(id)?.kind !== 'session')
|
|
2688
|
+
if (session === undefined || session.exited || this.mailbox.participant(id)?.kind !== 'session') {
|
|
2689
|
+
this.queuedMessageNudges.delete(id);
|
|
2658
2690
|
return 'done';
|
|
2691
|
+
}
|
|
2659
2692
|
const pendingIds = session.nudgePendingLine() !== null ? [...session.nudgePendingIds()] : [];
|
|
2660
2693
|
// A scheduled body has no mailbox ids. It can clear on proof of consumption,
|
|
2661
2694
|
// but a later inbox message can never authorise its submit key.
|
|
2662
2695
|
if (session.nudgePendingLine() !== null && pendingIds.length === 0 &&
|
|
2663
2696
|
!await this.clearScheduledBody(session))
|
|
2664
2697
|
return 'wait';
|
|
2665
|
-
const clearedIds = pendingIds.length > 0 && await session.clearConsumedNudge() ? pendingIds : [];
|
|
2666
2698
|
const unread = this.mailbox.inbox(id, { markRead: false });
|
|
2699
|
+
const stale = pendingIds.length > 0 &&
|
|
2700
|
+
pendingIds.every((messageId) => !unread.some((entry) => entry.id === messageId));
|
|
2701
|
+
if (stale) {
|
|
2702
|
+
const line = session.nudgePendingLine();
|
|
2703
|
+
if (line === null || !session.discardStalePendingNudge(line, pendingIds))
|
|
2704
|
+
return 'wait';
|
|
2705
|
+
this.audit.record('daemon', 'nudge-pending-stale', id, {
|
|
2706
|
+
messageIds: pendingIds, count: pendingIds.length,
|
|
2707
|
+
});
|
|
2708
|
+
}
|
|
2709
|
+
const clearedIds = !stale && pendingIds.length > 0 && await session.clearConsumedNudge() ? pendingIds : [];
|
|
2667
2710
|
if (clearedIds.length > 0) {
|
|
2668
2711
|
this.audit.record('daemon', 'nudge-cleared', id, { messageIds: clearedIds,
|
|
2669
2712
|
waitingMessageIds: unread.map((entry) => entry.id) });
|
|
2670
2713
|
}
|
|
2714
|
+
// Resolve a retained line before the queued-message hold below. It may
|
|
2715
|
+
// receive its own submit key, but another body must never be typed over it.
|
|
2671
2716
|
const pending = session.nudgePendingLine();
|
|
2672
2717
|
if (pending !== null) {
|
|
2673
2718
|
if (session.nudgePendingIds().length === 0)
|
|
2674
2719
|
return 'wait';
|
|
2720
|
+
const covered = session.nudgePendingIds();
|
|
2721
|
+
const auditPending = (reason) => {
|
|
2722
|
+
if (session.nudgePendingAuditOnce())
|
|
2723
|
+
this.audit.record('daemon', 'nudge-pending', id, {
|
|
2724
|
+
messageIds: [...covered], count: covered.length, reason,
|
|
2725
|
+
});
|
|
2726
|
+
};
|
|
2675
2727
|
const ids = unread.filter((entry) => entry.state === 'waiting').map((entry) => entry.id);
|
|
2676
|
-
if (ids.length === 0)
|
|
2728
|
+
if (ids.length === 0) {
|
|
2729
|
+
auditPending('messages-changed');
|
|
2677
2730
|
return 'done';
|
|
2678
|
-
|
|
2679
|
-
if (!
|
|
2731
|
+
}
|
|
2732
|
+
if (!covered.every((messageId) => unread.some((entry) => entry.id === messageId))) {
|
|
2733
|
+
auditPending('messages-changed');
|
|
2734
|
+
return 'wait';
|
|
2735
|
+
}
|
|
2736
|
+
if (!session.nudgePendingSafeToSubmit()) {
|
|
2680
2737
|
if (session.nudgeReadyWithLine(pending) && session.nudgePendingTouchRefusal()) {
|
|
2681
2738
|
this.audit.record('daemon', 'nudge-aborted', id, {
|
|
2682
2739
|
messageIds: [...covered], count: covered.length, reason: 'person-input-since',
|
|
2740
|
+
...session.nudgePendingBodyMeta(),
|
|
2683
2741
|
});
|
|
2684
2742
|
}
|
|
2743
|
+
auditPending('person-input-since');
|
|
2685
2744
|
return 'wait';
|
|
2686
2745
|
}
|
|
2687
|
-
if (!session.nudgeReadyWithLine(pending))
|
|
2746
|
+
if (!session.nudgeReadyWithLine(pending)) {
|
|
2747
|
+
auditPending('line-not-seen');
|
|
2688
2748
|
return 'wait';
|
|
2749
|
+
}
|
|
2689
2750
|
const inputVersion = session.nudgeInputVersion();
|
|
2690
2751
|
const queuedInputVersion = session.nudgeQueuedInputVersion();
|
|
2691
2752
|
const screenRevision = session.nudgeScreenRevision();
|
|
@@ -2693,10 +2754,13 @@ export class Daemon {
|
|
|
2693
2754
|
const waitingIds = this.mailbox.inbox(id, { markRead: false })
|
|
2694
2755
|
.filter((entry) => entry.state === 'waiting').map((entry) => entry.id);
|
|
2695
2756
|
return !session.exited && !session.getStalled() &&
|
|
2757
|
+
session.nudgePendingSafeToSubmit() &&
|
|
2696
2758
|
session.nudgeInputVersion() === inputVersion &&
|
|
2697
2759
|
session.nudgeQueuedInputVersion() === queuedInputVersion &&
|
|
2698
2760
|
session.nudgeScreenRevision() === screenRevision &&
|
|
2699
2761
|
session.answerablePrompt(screen) === null && session.nudgePendingIds().length > 0 &&
|
|
2762
|
+
covered.every((messageId) => this.mailbox.inbox(id, { markRead: false })
|
|
2763
|
+
.some((entry) => entry.id === messageId)) &&
|
|
2700
2764
|
waitingIds.length === ids.length &&
|
|
2701
2765
|
ids.every((messageId) => waitingIds.includes(messageId));
|
|
2702
2766
|
});
|
|
@@ -2704,8 +2768,10 @@ export class Daemon {
|
|
|
2704
2768
|
if (session.nudgePendingTouchRefusal()) {
|
|
2705
2769
|
this.audit.record('daemon', 'nudge-aborted', id, {
|
|
2706
2770
|
messageIds: [...covered], count: covered.length, reason: 'person-input-since',
|
|
2771
|
+
...session.nudgePendingBodyMeta(),
|
|
2707
2772
|
});
|
|
2708
2773
|
}
|
|
2774
|
+
auditPending('person-input-since');
|
|
2709
2775
|
return 'wait';
|
|
2710
2776
|
}
|
|
2711
2777
|
this.mailbox.markNudged(ids);
|
|
@@ -2714,25 +2780,48 @@ export class Daemon {
|
|
|
2714
2780
|
staleMessageIds: covered.filter((messageId) => !unread.some((entry) => entry.id === messageId)) });
|
|
2715
2781
|
return 'sent';
|
|
2716
2782
|
}
|
|
2783
|
+
const queued = this.queuedMessageNudges?.get(id);
|
|
2784
|
+
if (queued !== undefined) {
|
|
2785
|
+
const state = session.nudgeState(this.now());
|
|
2786
|
+
if (this.now() - queued.at >= QUEUED_NUDGE_MAX_MS || (state.ready && !state.queued)) {
|
|
2787
|
+
this.queuedMessageNudges.delete(id);
|
|
2788
|
+
}
|
|
2789
|
+
else {
|
|
2790
|
+
const waitingIds = unread.filter((entry) => entry.state === 'waiting').map((entry) => entry.id);
|
|
2791
|
+
const newlyHeld = waitingIds.filter((messageId) => !queued.heldIds.has(messageId));
|
|
2792
|
+
if (newlyHeld.length > 0) {
|
|
2793
|
+
for (const messageId of newlyHeld)
|
|
2794
|
+
queued.heldIds.add(messageId);
|
|
2795
|
+
this.audit.record('daemon', 'nudge-held', id, { reason: 'queued-nudge-pending',
|
|
2796
|
+
messageIds: newlyHeld, count: newlyHeld.length, queuedMessageIds: queued.messageIds });
|
|
2797
|
+
}
|
|
2798
|
+
return waitingIds.length > 0 ? 'wait' : 'done';
|
|
2799
|
+
}
|
|
2800
|
+
}
|
|
2717
2801
|
// Only a message that has not been nudged yet starts a nudge. One that was
|
|
2718
2802
|
// nudged but is still unread (the agent did not read its inbox) is listed
|
|
2719
|
-
// in the line again when a later message arrives
|
|
2803
|
+
// in the line again when a later message arrives after any queued hold ends.
|
|
2720
2804
|
const ids = unread.filter((entry) => entry.state === 'waiting').map((entry) => entry.id);
|
|
2721
2805
|
if (ids.length === 0)
|
|
2722
2806
|
return 'done';
|
|
2723
2807
|
if (!session.hasPainted() || !session.nudgeReady(this.now()))
|
|
2724
2808
|
return 'wait';
|
|
2725
2809
|
const line = buildNudge(unread);
|
|
2810
|
+
const readVersion = this.inboxReadVersions?.get(id) ?? 0;
|
|
2726
2811
|
const result = await this.deliverLine(session, line, undefined, ids, () => {
|
|
2727
2812
|
const current = this.mailbox.inbox(id, { markRead: false });
|
|
2728
2813
|
return ids.every(messageId => current.some(entry => entry.id === messageId && entry.state === 'waiting'));
|
|
2729
2814
|
});
|
|
2730
2815
|
if (result.outcome === 'aborted') {
|
|
2731
|
-
this.audit.record('daemon', 'nudge-aborted', id, { messageIds: ids, count: ids.length, reason: result.reason
|
|
2816
|
+
this.audit.record('daemon', 'nudge-aborted', id, { messageIds: ids, count: ids.length, reason: result.reason,
|
|
2817
|
+
...session.nudgePendingBodyMeta() });
|
|
2732
2818
|
}
|
|
2733
2819
|
if (result.outcome !== 'sent' && result.outcome !== 'queued')
|
|
2734
2820
|
return 'wait';
|
|
2735
2821
|
this.mailbox.markNudged(ids);
|
|
2822
|
+
if (result.outcome === 'queued' && (this.inboxReadVersions?.get(id) ?? 0) === readVersion) {
|
|
2823
|
+
this.queuedMessageNudges.set(id, { at: this.now(), messageIds: ids, heldIds: new Set() });
|
|
2824
|
+
}
|
|
2736
2825
|
this.audit.record('daemon', 'nudge', id, { messageIds: ids, count: ids.length,
|
|
2737
2826
|
...(result.outcome === 'queued' ? { queued: true } : {}) });
|
|
2738
2827
|
return 'sent';
|
|
@@ -2840,6 +2929,14 @@ export class Daemon {
|
|
|
2840
2929
|
}
|
|
2841
2930
|
}
|
|
2842
2931
|
catch (error) {
|
|
2932
|
+
const pendingBody = session.nudgePendingBodyMeta();
|
|
2933
|
+
if (ids.length > 0 && pendingBody.pendingBody) {
|
|
2934
|
+
const reason = error instanceof SessionInputFailedError ? 'input-failed'
|
|
2935
|
+
: error instanceof SessionProgramEndedError ? 'session-ended' : 'write-failed';
|
|
2936
|
+
this.audit.record('daemon', 'nudge-aborted', session.id, {
|
|
2937
|
+
messageIds: ids, count: ids.length, reason, ...pendingBody,
|
|
2938
|
+
});
|
|
2939
|
+
}
|
|
2843
2940
|
if (error instanceof SessionInputFailedError)
|
|
2844
2941
|
return { outcome: 'skipped', reason: 'input-failed' };
|
|
2845
2942
|
if (error instanceof SessionProgramEndedError)
|
|
@@ -3142,6 +3239,18 @@ export class Daemon {
|
|
|
3142
3239
|
await this.launchProfile(req, res, { launcher: caller });
|
|
3143
3240
|
return;
|
|
3144
3241
|
}
|
|
3242
|
+
if (req.method === 'GET' && url.pathname === '/messages/unread-age') {
|
|
3243
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
3244
|
+
if (this.callerContext.getStore() !== undefined ||
|
|
3245
|
+
req.headers['x-shellpuppet-inside-session'] !== undefined ||
|
|
3246
|
+
req.headers['x-shellpuppet-claim'] !== undefined ||
|
|
3247
|
+
req.headers['x-shellpuppet-claim-secret'] !== undefined) {
|
|
3248
|
+
json(res, 403, { error: 'Owner access is required.' });
|
|
3249
|
+
return;
|
|
3250
|
+
}
|
|
3251
|
+
this.writeUnreadAge(res, actor);
|
|
3252
|
+
return;
|
|
3253
|
+
}
|
|
3145
3254
|
if (segments[0] === 'messages') {
|
|
3146
3255
|
await this.handleMessages(req, res, url, segments, actor);
|
|
3147
3256
|
return;
|
|
@@ -3537,6 +3646,8 @@ export class Daemon {
|
|
|
3537
3646
|
json(res, 400, { error: 'data (string) is required.' });
|
|
3538
3647
|
return;
|
|
3539
3648
|
}
|
|
3649
|
+
const report = (submitMode === undefined || submitMode === 'off') && body['submit'] !== true
|
|
3650
|
+
? terminalReportBatchKind(data) : null;
|
|
3540
3651
|
// Awaited: long input is chunked (Session.write), so verify below must
|
|
3541
3652
|
// not read the screen until every chunk has actually been written.
|
|
3542
3653
|
// The write resolves with the screen as it was just before this input
|
|
@@ -3553,9 +3664,9 @@ export class Daemon {
|
|
|
3553
3664
|
const submit = body['submit'] === true;
|
|
3554
3665
|
let written;
|
|
3555
3666
|
try {
|
|
3556
|
-
written =
|
|
3557
|
-
?
|
|
3558
|
-
|
|
3667
|
+
written = await session.writeWithResult(data, {
|
|
3668
|
+
...(submitMode !== undefined ? { submitMode } : {}), terminalReport: report !== null,
|
|
3669
|
+
});
|
|
3559
3670
|
}
|
|
3560
3671
|
catch (error) {
|
|
3561
3672
|
json(res, 409, {
|
|
@@ -3570,9 +3681,15 @@ export class Daemon {
|
|
|
3570
3681
|
? { outcome: 'not-requested' }
|
|
3571
3682
|
: submitOutcome(written, true);
|
|
3572
3683
|
const bytes = submitMode === undefined ? data.length : written.inputBytes ?? 0;
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3684
|
+
if (report !== null) {
|
|
3685
|
+
this.audit.record(actor, 'report', id, { kind: report, chars: bytes,
|
|
3686
|
+
...(this.audit.captureContent ? { data } : {}) });
|
|
3687
|
+
}
|
|
3688
|
+
else {
|
|
3689
|
+
this.audit.record(actor, 'input', id, { chars: bytes,
|
|
3690
|
+
...(submitMode !== undefined ? { submitMode, ...reporting } : reporting ? { submit: true, ...reporting } : {}) });
|
|
3691
|
+
this.broadcastControl(id, { type: 'input-source', source: 'agent' });
|
|
3692
|
+
}
|
|
3576
3693
|
// §3.1: TUIs silently swallow input while loading or when a panel has
|
|
3577
3694
|
// stolen focus. verify re-reads the screen and reports whether the
|
|
3578
3695
|
// text landed (best effort: hidden-input prompts and heavy
|
|
@@ -5082,7 +5199,7 @@ export class Daemon {
|
|
|
5082
5199
|
* exit notice); TEXT frames are JSON control messages. Incoming text frames
|
|
5083
5200
|
* are either JSON controls (resize) or bytes for the program: a person's
|
|
5084
5201
|
* keystrokes, or the viewer's terminal answering the program
|
|
5085
|
-
* (terminalReports.ts).
|
|
5202
|
+
* (terminalReports.ts). Incoming binary frames carry raw onBinary input.
|
|
5086
5203
|
*/
|
|
5087
5204
|
attachSocket(ws, session, credential, options = {}) {
|
|
5088
5205
|
const { readOnly } = credential;
|
|
@@ -5250,120 +5367,163 @@ export class Daemon {
|
|
|
5250
5367
|
});
|
|
5251
5368
|
/** Whether this socket has been told that the session's input has failed. */
|
|
5252
5369
|
let toldInputFailed = false;
|
|
5253
|
-
const onViewerMessage = async (message) => {
|
|
5370
|
+
const onViewerMessage = async (message, isBinary) => {
|
|
5254
5371
|
this.lastActivity = Date.now();
|
|
5255
|
-
const
|
|
5256
|
-
|
|
5257
|
-
|
|
5258
|
-
|
|
5259
|
-
parsed
|
|
5260
|
-
|
|
5261
|
-
|
|
5262
|
-
|
|
5263
|
-
|
|
5264
|
-
|
|
5265
|
-
|
|
5266
|
-
|
|
5267
|
-
this.
|
|
5268
|
-
|
|
5269
|
-
|
|
5270
|
-
|
|
5271
|
-
|
|
5272
|
-
|
|
5273
|
-
}
|
|
5274
|
-
if (typeof parsed === 'object' && parsed !== null &&
|
|
5275
|
-
parsed.type === 'layout') {
|
|
5276
|
-
const notice = parsed;
|
|
5277
|
-
if (credential.kind === 'dashboard' && this.dashboardFeedCurrent(ws) &&
|
|
5278
|
-
Number.isSafeInteger(notice.layoutId) && Number(notice.layoutId) > 0 &&
|
|
5279
|
-
typeof notice.eligible === 'boolean') {
|
|
5280
|
-
const id = Number(notice.layoutId);
|
|
5281
|
-
if (id > (this.dashboardLayouts.get(ws)?.id ?? 0)) {
|
|
5282
|
-
const prior = this.dashboardLayouts.get(ws);
|
|
5283
|
-
const real = notice.real === true;
|
|
5284
|
-
this.dashboardLayouts.set(ws, { id, realId: real ? id : (prior?.realId ?? 0),
|
|
5285
|
-
order: real ? this.nextGeometryOrder(session.id) : (prior?.order ?? 0),
|
|
5286
|
-
eligible: notice.eligible });
|
|
5372
|
+
const bytes = isBinary ? Buffer.from(message) : null;
|
|
5373
|
+
const text = bytes === null ? (typeof message === 'string' ? message : message.toString('utf8')) : '';
|
|
5374
|
+
if (!isBinary)
|
|
5375
|
+
try {
|
|
5376
|
+
const parsed = JSON.parse(text);
|
|
5377
|
+
if (typeof parsed === 'object' && parsed !== null &&
|
|
5378
|
+
parsed.type === 'release-grid') {
|
|
5379
|
+
// Only the current controller can give up a dashboard grid. Another
|
|
5380
|
+
// document with the same cookie cannot make a live controller yield.
|
|
5381
|
+
if (credential.kind === 'dashboard' && !session.exited && !session.geometryHeld &&
|
|
5382
|
+
this.geometryController.get(session.id) === ws) {
|
|
5383
|
+
const windows = this.dashboardWindows();
|
|
5384
|
+
const target = windows.find(({ pair }) => this.dashboardAssignments.get(pair)?.includes(session.id));
|
|
5385
|
+
if (target)
|
|
5386
|
+
this.dashboardTransferable.set(session.id, target.socket.protocol);
|
|
5387
|
+
else if (windows.length === 0)
|
|
5388
|
+
this.dashboardTransferable.set(session.id, null);
|
|
5389
|
+
this.announceGeometry(session.id, session, { resync: false });
|
|
5287
5390
|
}
|
|
5288
|
-
}
|
|
5289
|
-
return;
|
|
5290
|
-
}
|
|
5291
|
-
if (typeof parsed === 'object' &&
|
|
5292
|
-
parsed !== null &&
|
|
5293
|
-
parsed.type === 'resize') {
|
|
5294
|
-
const { cols, rows, take, acceptOffer, automatic, takeId, choiceRevision, layoutId } = parsed;
|
|
5295
|
-
const autoTake = automatic === true && take === true && acceptOffer !== true &&
|
|
5296
|
-
credential.kind === 'dashboard';
|
|
5297
|
-
const requestId = Number.isSafeInteger(takeId) && Number(takeId) > 0 ? Number(takeId) : undefined;
|
|
5298
|
-
const refuseAuto = (staleLayoutId = false) => {
|
|
5299
|
-
if (ws.readyState === ws.OPEN)
|
|
5300
|
-
ws.send(JSON.stringify({ ...this.geometryFrame(session, ws),
|
|
5301
|
-
refusedTakeId: requestId, ...(staleLayoutId ? { staleLayoutId: true } : {}) }));
|
|
5302
|
-
};
|
|
5303
|
-
if (automatic === true && (!autoTake || requestId === undefined ||
|
|
5304
|
-
!Number.isSafeInteger(choiceRevision) || !Number.isSafeInteger(layoutId))) {
|
|
5305
|
-
refuseAuto();
|
|
5306
5391
|
return;
|
|
5307
5392
|
}
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5393
|
+
if (typeof parsed === 'object' && parsed !== null &&
|
|
5394
|
+
parsed.type === 'layout') {
|
|
5395
|
+
const notice = parsed;
|
|
5396
|
+
if (credential.kind === 'dashboard' && this.dashboardFeedCurrent(ws) &&
|
|
5397
|
+
Number.isSafeInteger(notice.layoutId) && Number(notice.layoutId) > 0 &&
|
|
5398
|
+
typeof notice.eligible === 'boolean') {
|
|
5399
|
+
const id = Number(notice.layoutId);
|
|
5400
|
+
if (id > (this.dashboardLayouts.get(ws)?.id ?? 0)) {
|
|
5401
|
+
const prior = this.dashboardLayouts.get(ws);
|
|
5402
|
+
const real = notice.real === true;
|
|
5403
|
+
this.dashboardLayouts.set(ws, { id, realId: real ? id : (prior?.realId ?? 0),
|
|
5404
|
+
order: real ? this.nextGeometryOrder(session.id) : (prior?.order ?? 0),
|
|
5405
|
+
eligible: notice.eligible });
|
|
5406
|
+
}
|
|
5407
|
+
}
|
|
5314
5408
|
return;
|
|
5315
|
-
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
5319
|
-
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
|
|
5326
|
-
|
|
5327
|
-
|
|
5328
|
-
|
|
5329
|
-
|
|
5330
|
-
|
|
5331
|
-
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5409
|
+
}
|
|
5410
|
+
if (typeof parsed === 'object' &&
|
|
5411
|
+
parsed !== null &&
|
|
5412
|
+
parsed.type === 'resize') {
|
|
5413
|
+
const { cols, rows, take, acceptOffer, automatic, takeId, choiceRevision, layoutId } = parsed;
|
|
5414
|
+
const autoTake = automatic === true && take === true && acceptOffer !== true &&
|
|
5415
|
+
credential.kind === 'dashboard';
|
|
5416
|
+
const requestId = Number.isSafeInteger(takeId) && Number(takeId) > 0 ? Number(takeId) : undefined;
|
|
5417
|
+
const refuseAuto = (staleLayoutId = false) => {
|
|
5418
|
+
if (ws.readyState === ws.OPEN)
|
|
5419
|
+
ws.send(JSON.stringify({ ...this.geometryFrame(session, ws),
|
|
5420
|
+
refusedTakeId: requestId, ...(staleLayoutId ? { staleLayoutId: true } : {}) }));
|
|
5421
|
+
};
|
|
5422
|
+
if (automatic === true && (!autoTake || requestId === undefined ||
|
|
5423
|
+
!Number.isSafeInteger(choiceRevision) || !Number.isSafeInteger(layoutId))) {
|
|
5424
|
+
refuseAuto();
|
|
5332
5425
|
return;
|
|
5333
5426
|
}
|
|
5334
|
-
|
|
5335
|
-
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
|
|
5339
|
-
|
|
5427
|
+
// Fit, Enlarge, Focus and Restore are a resize that says `take`.
|
|
5428
|
+
// A passive socket's other resizes, including one a dashboard pane
|
|
5429
|
+
// sends because the window moved, are not that act: the grid stays,
|
|
5430
|
+
// nobody is handed it, and nothing is audited. Passivity is the
|
|
5431
|
+
// dashboard credential, or `drive=0` on a viewer or client socket.
|
|
5432
|
+
if (passive && take !== true)
|
|
5433
|
+
return;
|
|
5434
|
+
if (!readOnly &&
|
|
5435
|
+
typeof cols === 'number' &&
|
|
5436
|
+
typeof rows === 'number' &&
|
|
5437
|
+
Number.isInteger(cols) &&
|
|
5438
|
+
Number.isInteger(rows) &&
|
|
5439
|
+
cols >= 2 &&
|
|
5440
|
+
cols <= 1_000 &&
|
|
5441
|
+
rows >= 2 &&
|
|
5442
|
+
rows <= 500 &&
|
|
5443
|
+
!session.exited) {
|
|
5444
|
+
if (session.geometryHeld) {
|
|
5445
|
+
// Held against viewports. Answer with the real grid so the page
|
|
5446
|
+
// stops proposing one it will never be given.
|
|
5447
|
+
if (autoTake)
|
|
5448
|
+
refuseAuto();
|
|
5449
|
+
else
|
|
5450
|
+
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5451
|
+
return;
|
|
5452
|
+
}
|
|
5453
|
+
const wanted = {
|
|
5454
|
+
cols: Math.min(cols, MAX_VIEWPORT_COLS),
|
|
5455
|
+
rows: Math.min(rows, MAX_VIEWPORT_ROWS),
|
|
5456
|
+
};
|
|
5457
|
+
if (autoTake) {
|
|
5458
|
+
try {
|
|
5459
|
+
let applied = false;
|
|
5460
|
+
let changedAtApply = false;
|
|
5461
|
+
let staleLayoutId = false;
|
|
5462
|
+
const shouldApply = () => {
|
|
5463
|
+
if (session.geometryHeld || session.exited || ws.readyState !== ws.OPEN ||
|
|
5464
|
+
!this.sessionSockets.get(session.id)?.has(ws) || !this.dashboardFeedCurrent(ws))
|
|
5465
|
+
return false;
|
|
5466
|
+
const layout = this.dashboardLayouts.get(ws);
|
|
5467
|
+
if (Number(layoutId) !== (layout?.id ?? 0)) {
|
|
5468
|
+
staleLayoutId = true;
|
|
5469
|
+
return false;
|
|
5470
|
+
}
|
|
5471
|
+
if (layout?.eligible === false)
|
|
5472
|
+
return false;
|
|
5473
|
+
const ownerPresent = this.hasGeometryController(session.id);
|
|
5474
|
+
if (ownerPresent && this.geometryController.get(session.id) !== ws)
|
|
5475
|
+
return false;
|
|
5476
|
+
const reserved = this.hasDashboardGeometryOwner(session.id);
|
|
5477
|
+
if (reserved && this.geometryController.get(session.id) !== ws)
|
|
5478
|
+
return false;
|
|
5479
|
+
const explicit = this.explicitResizeRevision.get(session.id) ?? 0;
|
|
5480
|
+
if (explicit > (this.controllerChoiceRevision.get(session.id) ?? 0) &&
|
|
5481
|
+
(layout?.order ?? 0) <= explicit)
|
|
5482
|
+
return false;
|
|
5483
|
+
const current = this.choiceRevision.get(session.id) ?? 0;
|
|
5484
|
+
if (Number(choiceRevision) !== current &&
|
|
5485
|
+
!(layout !== undefined && layout.id === Number(layoutId) && layout.order > current))
|
|
5486
|
+
return false;
|
|
5487
|
+
changedAtApply = wanted.cols !== session.cols || wanted.rows !== session.rows;
|
|
5488
|
+
return true;
|
|
5489
|
+
};
|
|
5490
|
+
const onApplied = () => {
|
|
5491
|
+
applied = true;
|
|
5492
|
+
const handedOver = this.geometryController.get(session.id) !== ws;
|
|
5493
|
+
this.geometryController.set(session.id, ws);
|
|
5494
|
+
this.dashboardGeometryOwner.set(session.id, ws.protocol);
|
|
5495
|
+
this.dashboardCredentialOwner.set(session.id, credential.dashboardToken);
|
|
5496
|
+
this.dashboardTransferable.delete(session.id);
|
|
5497
|
+
this.recordGeometryChoice(session.id, true);
|
|
5498
|
+
this.geometryEpoch.set(session.id, (this.geometryEpoch.get(session.id) ?? 0) + 1);
|
|
5499
|
+
if (changedAtApply)
|
|
5500
|
+
this.audit.record(actor, 'resize', session.id, {
|
|
5501
|
+
cols: wanted.cols, rows: wanted.rows, hold: false,
|
|
5502
|
+
});
|
|
5503
|
+
if (changedAtApply || handedOver)
|
|
5504
|
+
this.announceGeometry(session.id, session, { resync: changedAtApply, acceptedTakeId: requestId });
|
|
5505
|
+
else
|
|
5506
|
+
ws.send(JSON.stringify({ ...this.geometryFrame(session, ws), acceptedTakeId: requestId }));
|
|
5507
|
+
};
|
|
5508
|
+
await session.resize(wanted.cols, wanted.rows, { shouldApply, skipIfSame: true, onApplied });
|
|
5509
|
+
if (!applied)
|
|
5510
|
+
refuseAuto(staleLayoutId);
|
|
5511
|
+
}
|
|
5512
|
+
catch (error) {
|
|
5513
|
+
refuseAuto();
|
|
5514
|
+
if (!(error instanceof SessionProgramEndedError))
|
|
5515
|
+
this.logger.warn('viewer resize failed', {
|
|
5516
|
+
id: session.id, error: String(error),
|
|
5517
|
+
});
|
|
5518
|
+
}
|
|
5519
|
+
return;
|
|
5520
|
+
}
|
|
5521
|
+
if (!this.widerReads.has(session.id) && acceptOffer !== true) {
|
|
5340
5522
|
let applied = false;
|
|
5341
5523
|
let changedAtApply = false;
|
|
5342
|
-
let staleLayoutId = false;
|
|
5343
5524
|
const shouldApply = () => {
|
|
5344
5525
|
if (session.geometryHeld || session.exited || ws.readyState !== ws.OPEN ||
|
|
5345
|
-
!this.sessionSockets.get(session.id)?.has(ws)
|
|
5346
|
-
return false;
|
|
5347
|
-
const layout = this.dashboardLayouts.get(ws);
|
|
5348
|
-
if (Number(layoutId) !== (layout?.id ?? 0)) {
|
|
5349
|
-
staleLayoutId = true;
|
|
5350
|
-
return false;
|
|
5351
|
-
}
|
|
5352
|
-
if (layout?.eligible === false)
|
|
5353
|
-
return false;
|
|
5354
|
-
const ownerPresent = this.hasGeometryController(session.id);
|
|
5355
|
-
if (ownerPresent && this.geometryController.get(session.id) !== ws)
|
|
5356
|
-
return false;
|
|
5357
|
-
const reserved = this.hasDashboardGeometryOwner(session.id);
|
|
5358
|
-
if (reserved && this.geometryController.get(session.id) !== ws)
|
|
5359
|
-
return false;
|
|
5360
|
-
const explicit = this.explicitResizeRevision.get(session.id) ?? 0;
|
|
5361
|
-
if (explicit > (this.controllerChoiceRevision.get(session.id) ?? 0) &&
|
|
5362
|
-
(layout?.order ?? 0) <= explicit)
|
|
5363
|
-
return false;
|
|
5364
|
-
const current = this.choiceRevision.get(session.id) ?? 0;
|
|
5365
|
-
if (Number(choiceRevision) !== current &&
|
|
5366
|
-
!(layout !== undefined && layout.id === Number(layoutId) && layout.order > current))
|
|
5526
|
+
!this.sessionSockets.get(session.id)?.has(ws))
|
|
5367
5527
|
return false;
|
|
5368
5528
|
changedAtApply = wanted.cols !== session.cols || wanted.rows !== session.rows;
|
|
5369
5529
|
return true;
|
|
@@ -5372,149 +5532,108 @@ export class Daemon {
|
|
|
5372
5532
|
applied = true;
|
|
5373
5533
|
const handedOver = this.geometryController.get(session.id) !== ws;
|
|
5374
5534
|
this.geometryController.set(session.id, ws);
|
|
5375
|
-
this.dashboardGeometryOwner.set(session.id, ws.protocol);
|
|
5376
|
-
this.dashboardCredentialOwner.set(session.id, credential.dashboardToken);
|
|
5377
|
-
this.dashboardTransferable.delete(session.id);
|
|
5378
5535
|
this.recordGeometryChoice(session.id, true);
|
|
5536
|
+
this.dashboardTransferable.delete(session.id);
|
|
5379
5537
|
this.geometryEpoch.set(session.id, (this.geometryEpoch.get(session.id) ?? 0) + 1);
|
|
5380
|
-
if (
|
|
5538
|
+
if (credential.kind === 'dashboard') {
|
|
5539
|
+
this.dashboardGeometryOwner.set(session.id, ws.protocol);
|
|
5540
|
+
this.dashboardCredentialOwner.set(session.id, credential.dashboardToken);
|
|
5541
|
+
}
|
|
5542
|
+
else {
|
|
5543
|
+
this.dashboardGeometryOwner.delete(session.id);
|
|
5544
|
+
this.dashboardCredentialOwner.delete(session.id);
|
|
5545
|
+
}
|
|
5546
|
+
if (changedAtApply) {
|
|
5381
5547
|
this.audit.record(actor, 'resize', session.id, {
|
|
5382
5548
|
cols: wanted.cols, rows: wanted.rows, hold: false,
|
|
5383
5549
|
});
|
|
5384
|
-
|
|
5385
|
-
|
|
5386
|
-
else
|
|
5387
|
-
|
|
5550
|
+
this.announceGeometry(session.id, session);
|
|
5551
|
+
}
|
|
5552
|
+
else if (handedOver)
|
|
5553
|
+
this.announceGeometry(session.id, session, { resync: false });
|
|
5554
|
+
else if (take === true)
|
|
5555
|
+
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5388
5556
|
};
|
|
5389
|
-
|
|
5390
|
-
|
|
5391
|
-
|
|
5392
|
-
|
|
5393
|
-
|
|
5394
|
-
|
|
5395
|
-
|
|
5396
|
-
|
|
5397
|
-
|
|
5398
|
-
});
|
|
5399
|
-
}
|
|
5400
|
-
return;
|
|
5401
|
-
}
|
|
5402
|
-
if (!this.widerReads.has(session.id) && acceptOffer !== true) {
|
|
5403
|
-
let applied = false;
|
|
5404
|
-
let changedAtApply = false;
|
|
5405
|
-
const shouldApply = () => {
|
|
5406
|
-
if (session.geometryHeld || session.exited || ws.readyState !== ws.OPEN ||
|
|
5407
|
-
!this.sessionSockets.get(session.id)?.has(ws))
|
|
5408
|
-
return false;
|
|
5409
|
-
changedAtApply = wanted.cols !== session.cols || wanted.rows !== session.rows;
|
|
5410
|
-
return true;
|
|
5411
|
-
};
|
|
5412
|
-
const onApplied = () => {
|
|
5413
|
-
applied = true;
|
|
5414
|
-
const handedOver = this.geometryController.get(session.id) !== ws;
|
|
5415
|
-
this.geometryController.set(session.id, ws);
|
|
5416
|
-
this.recordGeometryChoice(session.id, true);
|
|
5417
|
-
this.dashboardTransferable.delete(session.id);
|
|
5418
|
-
this.geometryEpoch.set(session.id, (this.geometryEpoch.get(session.id) ?? 0) + 1);
|
|
5419
|
-
if (credential.kind === 'dashboard') {
|
|
5420
|
-
this.dashboardGeometryOwner.set(session.id, ws.protocol);
|
|
5421
|
-
this.dashboardCredentialOwner.set(session.id, credential.dashboardToken);
|
|
5422
|
-
}
|
|
5423
|
-
else {
|
|
5424
|
-
this.dashboardGeometryOwner.delete(session.id);
|
|
5425
|
-
this.dashboardCredentialOwner.delete(session.id);
|
|
5557
|
+
try {
|
|
5558
|
+
if (wanted.cols !== session.cols || wanted.rows !== session.rows) {
|
|
5559
|
+
await session.resize(wanted.cols, wanted.rows, { shouldApply, skipIfSame: true, onApplied });
|
|
5560
|
+
}
|
|
5561
|
+
else {
|
|
5562
|
+
await session.resizeIfCurrent(wanted.cols, wanted.rows, shouldApply, () => onApplied(), true);
|
|
5563
|
+
}
|
|
5564
|
+
if (!applied && ws.readyState === ws.OPEN)
|
|
5565
|
+
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5426
5566
|
}
|
|
5427
|
-
|
|
5428
|
-
|
|
5429
|
-
|
|
5430
|
-
});
|
|
5431
|
-
this.announceGeometry(session.id, session);
|
|
5567
|
+
catch (error) {
|
|
5568
|
+
if (error instanceof SessionProgramEndedError)
|
|
5569
|
+
return;
|
|
5570
|
+
this.logger.warn('viewer resize failed', { id: session.id, error: String(error) });
|
|
5432
5571
|
}
|
|
5433
|
-
|
|
5434
|
-
|
|
5435
|
-
else if (take === true)
|
|
5436
|
-
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5437
|
-
};
|
|
5572
|
+
return;
|
|
5573
|
+
}
|
|
5438
5574
|
try {
|
|
5439
|
-
|
|
5440
|
-
|
|
5441
|
-
|
|
5442
|
-
|
|
5443
|
-
|
|
5444
|
-
|
|
5575
|
+
const applied = await session.resizeIfCurrent(wanted.cols, wanted.rows,
|
|
5576
|
+
// A viewport that left while its take waited in the queue made
|
|
5577
|
+
// no lasting choice: skip it so the pending wider restore runs.
|
|
5578
|
+
() => !session.geometryHeld && !session.exited && ws.readyState === ws.OPEN &&
|
|
5579
|
+
(acceptOffer !== true || this.gridOfferedTo(session.id, ws)) &&
|
|
5580
|
+
(this.sessionSockets.get(session.id)?.has(ws) ?? false), (changed) => {
|
|
5581
|
+
const handedOver = this.geometryController.get(session.id) !== ws;
|
|
5582
|
+
// The controller change and epoch belong to the same queued
|
|
5583
|
+
// act as the resize, before a queued wider restore can run.
|
|
5584
|
+
this.geometryController.set(session.id, ws);
|
|
5585
|
+
this.recordGeometryChoice(session.id, true);
|
|
5586
|
+
this.dashboardTransferable.delete(session.id);
|
|
5587
|
+
this.geometryEpoch.set(session.id, (this.geometryEpoch.get(session.id) ?? 0) + 1);
|
|
5588
|
+
if (credential.kind === 'dashboard') {
|
|
5589
|
+
this.dashboardGeometryOwner.set(session.id, ws.protocol);
|
|
5590
|
+
this.dashboardCredentialOwner.set(session.id, credential.dashboardToken);
|
|
5591
|
+
}
|
|
5592
|
+
else {
|
|
5593
|
+
this.dashboardGeometryOwner.delete(session.id);
|
|
5594
|
+
this.dashboardCredentialOwner.delete(session.id);
|
|
5595
|
+
}
|
|
5596
|
+
if (changed) {
|
|
5597
|
+
this.audit.record(actor, 'resize', session.id, {
|
|
5598
|
+
cols: wanted.cols, rows: wanted.rows, hold: false,
|
|
5599
|
+
});
|
|
5600
|
+
this.announceGeometry(session.id, session);
|
|
5601
|
+
}
|
|
5602
|
+
else if (handedOver)
|
|
5603
|
+
this.announceGeometry(session.id, session, { resync: false });
|
|
5604
|
+
else if (take === true)
|
|
5605
|
+
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5606
|
+
}, true);
|
|
5445
5607
|
if (!applied && ws.readyState === ws.OPEN)
|
|
5446
5608
|
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5447
5609
|
}
|
|
5448
5610
|
catch (error) {
|
|
5611
|
+
// The program has ended (a viewer fitting its window while
|
|
5612
|
+
// looking at a dying session), and the session's exit follows
|
|
5613
|
+
// within about a second, with an `exited` frame to every
|
|
5614
|
+
// watcher. Nothing moved and nobody takes the grid, so nothing
|
|
5615
|
+
// is said: an answer would have a driving page whose window
|
|
5616
|
+
// does not fit the grid propose again at once, over and over
|
|
5617
|
+
// until the exit arrives.
|
|
5449
5618
|
if (error instanceof SessionProgramEndedError)
|
|
5450
5619
|
return;
|
|
5451
|
-
|
|
5620
|
+
// Any other failure is logged rather than left to escape: an
|
|
5621
|
+
// exception from a WS handler would take the whole daemon down
|
|
5622
|
+
// via the uncaughtException path.
|
|
5623
|
+
this.logger.warn('viewer resize failed', {
|
|
5624
|
+
id: session.id,
|
|
5625
|
+
error: String(error),
|
|
5626
|
+
});
|
|
5452
5627
|
}
|
|
5453
|
-
return;
|
|
5454
|
-
}
|
|
5455
|
-
try {
|
|
5456
|
-
const applied = await session.resizeIfCurrent(wanted.cols, wanted.rows,
|
|
5457
|
-
// A viewport that left while its take waited in the queue made
|
|
5458
|
-
// no lasting choice: skip it so the pending wider restore runs.
|
|
5459
|
-
() => !session.geometryHeld && !session.exited && ws.readyState === ws.OPEN &&
|
|
5460
|
-
(acceptOffer !== true || this.gridOfferedTo(session.id, ws)) &&
|
|
5461
|
-
(this.sessionSockets.get(session.id)?.has(ws) ?? false), (changed) => {
|
|
5462
|
-
const handedOver = this.geometryController.get(session.id) !== ws;
|
|
5463
|
-
// The controller change and epoch belong to the same queued
|
|
5464
|
-
// act as the resize, before a queued wider restore can run.
|
|
5465
|
-
this.geometryController.set(session.id, ws);
|
|
5466
|
-
this.recordGeometryChoice(session.id, true);
|
|
5467
|
-
this.dashboardTransferable.delete(session.id);
|
|
5468
|
-
this.geometryEpoch.set(session.id, (this.geometryEpoch.get(session.id) ?? 0) + 1);
|
|
5469
|
-
if (credential.kind === 'dashboard') {
|
|
5470
|
-
this.dashboardGeometryOwner.set(session.id, ws.protocol);
|
|
5471
|
-
this.dashboardCredentialOwner.set(session.id, credential.dashboardToken);
|
|
5472
|
-
}
|
|
5473
|
-
else {
|
|
5474
|
-
this.dashboardGeometryOwner.delete(session.id);
|
|
5475
|
-
this.dashboardCredentialOwner.delete(session.id);
|
|
5476
|
-
}
|
|
5477
|
-
if (changed) {
|
|
5478
|
-
this.audit.record(actor, 'resize', session.id, {
|
|
5479
|
-
cols: wanted.cols, rows: wanted.rows, hold: false,
|
|
5480
|
-
});
|
|
5481
|
-
this.announceGeometry(session.id, session);
|
|
5482
|
-
}
|
|
5483
|
-
else if (handedOver)
|
|
5484
|
-
this.announceGeometry(session.id, session, { resync: false });
|
|
5485
|
-
else if (take === true)
|
|
5486
|
-
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5487
|
-
}, true);
|
|
5488
|
-
if (!applied && ws.readyState === ws.OPEN)
|
|
5489
|
-
ws.send(JSON.stringify(this.geometryFrame(session, ws)));
|
|
5490
|
-
}
|
|
5491
|
-
catch (error) {
|
|
5492
|
-
// The program has ended (a viewer fitting its window while
|
|
5493
|
-
// looking at a dying session), and the session's exit follows
|
|
5494
|
-
// within about a second, with an `exited` frame to every
|
|
5495
|
-
// watcher. Nothing moved and nobody takes the grid, so nothing
|
|
5496
|
-
// is said: an answer would have a driving page whose window
|
|
5497
|
-
// does not fit the grid propose again at once, over and over
|
|
5498
|
-
// until the exit arrives.
|
|
5499
|
-
if (error instanceof SessionProgramEndedError)
|
|
5500
|
-
return;
|
|
5501
|
-
// Any other failure is logged rather than left to escape: an
|
|
5502
|
-
// exception from a WS handler would take the whole daemon down
|
|
5503
|
-
// via the uncaughtException path.
|
|
5504
|
-
this.logger.warn('viewer resize failed', {
|
|
5505
|
-
id: session.id,
|
|
5506
|
-
error: String(error),
|
|
5507
|
-
});
|
|
5508
5628
|
}
|
|
5629
|
+
return;
|
|
5509
5630
|
}
|
|
5510
|
-
return;
|
|
5511
5631
|
}
|
|
5512
|
-
|
|
5513
|
-
|
|
5514
|
-
|
|
5515
|
-
|
|
5516
|
-
|
|
5517
|
-
}
|
|
5632
|
+
catch {
|
|
5633
|
+
// Not JSON: bytes for the program, a person's keystrokes or the
|
|
5634
|
+
// viewer's terminal answering the program. Straight into the PTY
|
|
5635
|
+
// either way: shared typing is the point of the tool.
|
|
5636
|
+
}
|
|
5518
5637
|
if (readOnly) {
|
|
5519
5638
|
// A watch-only credential. Dropped silently and on the daemon's side:
|
|
5520
5639
|
// the page has already been told it is read-only and disables its own
|
|
@@ -5554,8 +5673,12 @@ export class Daemon {
|
|
|
5554
5673
|
// sequences, so this is not always one keystroke.) .catch keeps the exit
|
|
5555
5674
|
// race, the session dying mid-write, from escaping as an unhandled
|
|
5556
5675
|
// rejection.
|
|
5557
|
-
const
|
|
5558
|
-
|
|
5676
|
+
const mouseReport = bytes !== null && bytes.length === 6 && bytes[0] === 0x1b &&
|
|
5677
|
+
bytes[1] === 0x5b && bytes[2] === 0x4d;
|
|
5678
|
+
const report = bytes === null ? terminalReportBatchKind(text) : mouseReport ? 'mouse' : null;
|
|
5679
|
+
void (bytes === null
|
|
5680
|
+
? session.write(text, { terminalReport: report !== null, humanInput: report === null })
|
|
5681
|
+
: session.writeBinary(bytes, { terminalReport: mouseReport })).catch((error) => {
|
|
5559
5682
|
this.logger.warn('viewer input failed', {
|
|
5560
5683
|
id: session.id,
|
|
5561
5684
|
error: String(error),
|
|
@@ -5569,21 +5692,21 @@ export class Daemon {
|
|
|
5569
5692
|
if (report !== null) {
|
|
5570
5693
|
this.audit.record(actor, 'report', session.id, {
|
|
5571
5694
|
kind: report,
|
|
5572
|
-
chars: text.length,
|
|
5573
|
-
...(this.audit.captureContent ? { data: text } : {}),
|
|
5695
|
+
chars: bytes?.length ?? text.length,
|
|
5696
|
+
...(this.audit.captureContent ? { data: bytes === null ? text : bytes.toString('latin1') } : {}),
|
|
5574
5697
|
});
|
|
5575
5698
|
return;
|
|
5576
5699
|
}
|
|
5577
5700
|
this.audit.record(actor, 'input', session.id, {
|
|
5578
|
-
chars: text.length,
|
|
5701
|
+
chars: bytes?.length ?? text.length,
|
|
5579
5702
|
});
|
|
5580
5703
|
this.broadcastControl(session.id, { type: 'input-source', source: 'human' });
|
|
5581
5704
|
};
|
|
5582
|
-
ws.on('message', (message) => {
|
|
5705
|
+
ws.on('message', (message, isBinary) => {
|
|
5583
5706
|
if (actor === 'dashboard' && ws.readyState !== ws.OPEN)
|
|
5584
5707
|
return;
|
|
5585
5708
|
this.callerContext.run(options.callerSession, () => {
|
|
5586
|
-
void onViewerMessage(message).catch((error) => {
|
|
5709
|
+
void onViewerMessage(message, isBinary).catch((error) => {
|
|
5587
5710
|
this.logger.warn('viewer message failed', { id: session.id, error: String(error) });
|
|
5588
5711
|
});
|
|
5589
5712
|
});
|