@fnndsc/calypso 0.27.1 → 0.28.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.
- package/dist/daemon/consoleSession.d.ts +10 -0
- package/dist/daemon/consoleSession.js +22 -0
- package/dist/daemon/consoleSession.js.map +1 -1
- package/dist/daemon/engine.d.ts +32 -34
- package/dist/daemon/lane.d.ts +48 -19
- package/dist/daemon/lane.js +81 -32
- package/dist/daemon/lane.js.map +1 -1
- package/dist/daemon/launch.js +4 -2
- package/dist/daemon/launch.js.map +1 -1
- package/dist/daemon/server.d.ts +54 -30
- package/dist/daemon/server.js +186 -105
- package/dist/daemon/server.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/notes.json +1 -1
- package/package.json +4 -4
package/dist/daemon/server.d.ts
CHANGED
|
@@ -149,9 +149,6 @@ export declare class CalypsoDaemon {
|
|
|
149
149
|
/** The last executed command's measured facts, sticky across pushes. */
|
|
150
150
|
private lastCommand;
|
|
151
151
|
private readonly telemetryProvider;
|
|
152
|
-
/** The line holding the lane, and since when; null while the lane is idle. */
|
|
153
|
-
private currentLine;
|
|
154
|
-
private currentStartedAt;
|
|
155
152
|
private telemetryTimer;
|
|
156
153
|
/** The index counts at the last heartbeat, to detect movement. */
|
|
157
154
|
private telemetryLastIndex;
|
|
@@ -189,22 +186,24 @@ export declare class CalypsoDaemon {
|
|
|
189
186
|
private regard;
|
|
190
187
|
/** Unsubscribes from the engine's ambient events; null until started. */
|
|
191
188
|
private ambientStop;
|
|
192
|
-
private currentOrigin;
|
|
193
|
-
private currentId;
|
|
194
189
|
/**
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* question it invented, so an ask raised under one is refused rather
|
|
199
|
-
* than shown.
|
|
190
|
+
* The line the code runs inside: several may run at once, so each keeps
|
|
191
|
+
* its own slot, and whatever it raises (output, a question, a pipe
|
|
192
|
+
* segment) goes to its own surface.
|
|
200
193
|
*/
|
|
201
|
-
private
|
|
202
|
-
/**
|
|
203
|
-
private
|
|
194
|
+
private readonly slotScope;
|
|
195
|
+
/** The lines running now, waiting aside or not, in the order they started. */
|
|
196
|
+
private readonly running;
|
|
197
|
+
/** Names each line for the engine. */
|
|
198
|
+
private lineCount;
|
|
204
199
|
/** Surfaces whose STOP found their command still waiting its turn, and when. */
|
|
205
200
|
private readonly stopPending;
|
|
206
|
-
/**
|
|
207
|
-
private
|
|
201
|
+
/** The surface of the line the code runs inside, or null outside any. */
|
|
202
|
+
private get currentOrigin();
|
|
203
|
+
/** The surface's id for the line the code runs inside, or null outside any. */
|
|
204
|
+
private get currentId();
|
|
205
|
+
/** Whether the line the code runs inside is surface-internal. */
|
|
206
|
+
private get currentInstrument();
|
|
208
207
|
private readonly prompts;
|
|
209
208
|
private readonly pipes;
|
|
210
209
|
private readonly shells;
|
|
@@ -271,6 +270,16 @@ export declare class CalypsoDaemon {
|
|
|
271
270
|
* @param response - The response to write.
|
|
272
271
|
*/
|
|
273
272
|
private backend_serve;
|
|
273
|
+
/**
|
|
274
|
+
* Says which joblets run now (`line &`): `{"joblets": [{number, line,
|
|
275
|
+
* waiting}]}`. A host about to restart the session (porter, for a new
|
|
276
|
+
* release) asks first, since a restart ends them (#1273). Gated by the
|
|
277
|
+
* attach token, as `/backend` is.
|
|
278
|
+
*
|
|
279
|
+
* @param request - The incoming HTTP request.
|
|
280
|
+
* @param response - The response to write.
|
|
281
|
+
*/
|
|
282
|
+
private joblets_serve;
|
|
274
283
|
/**
|
|
275
284
|
* Serves one ChRIS file's bytes over HTTP: `GET /vfs?path=...&token=...`.
|
|
276
285
|
*
|
|
@@ -446,8 +455,9 @@ export declare class CalypsoDaemon {
|
|
|
446
455
|
/**
|
|
447
456
|
* Relays an engine-originated event to every surface: a sampler's refreshed
|
|
448
457
|
* model rides the session bus tagged with the daemon as its surface (off
|
|
449
|
-
* the scrollback — it is state, not something the operator did),
|
|
450
|
-
* watch state change goes out as its own message
|
|
458
|
+
* the scrollback — it is state, not something the operator did), a
|
|
459
|
+
* watch state change goes out as its own message, and a numbering goes
|
|
460
|
+
* only to the surface whose line listed.
|
|
451
461
|
*
|
|
452
462
|
* @param event - The ambient event.
|
|
453
463
|
*/
|
|
@@ -485,30 +495,43 @@ export declare class CalypsoDaemon {
|
|
|
485
495
|
*/
|
|
486
496
|
private bus_publish;
|
|
487
497
|
/**
|
|
488
|
-
*
|
|
489
|
-
*
|
|
498
|
+
* What a line holds while it runs, as the engine reads it off the line: a
|
|
499
|
+
* read runs beside anything, and lines changing the same piece of the
|
|
500
|
+
* session take turns (#1273).
|
|
501
|
+
*
|
|
502
|
+
* @param line - The line.
|
|
503
|
+
* @returns What it holds.
|
|
504
|
+
*/
|
|
505
|
+
private lineWant_get;
|
|
506
|
+
/**
|
|
507
|
+
* Runs one line in its turn: it waits until nothing running holds what it
|
|
508
|
+
* changes, then runs in a slot of its own.
|
|
490
509
|
*
|
|
491
510
|
* @param origin - The surface that submitted the request.
|
|
492
511
|
* @param message - The execute request.
|
|
493
512
|
*/
|
|
494
|
-
private
|
|
513
|
+
private line_run;
|
|
495
514
|
/**
|
|
496
|
-
*
|
|
515
|
+
* Runs one execute request: replies to the requester with the result, and
|
|
516
|
+
* publishes each envelope to the session bus.
|
|
497
517
|
*
|
|
498
|
-
* @
|
|
518
|
+
* @param slot - The line's slot, holding its turn.
|
|
519
|
+
* @param message - The execute request.
|
|
499
520
|
*/
|
|
500
|
-
private
|
|
521
|
+
private execute_run;
|
|
501
522
|
/**
|
|
502
|
-
*
|
|
523
|
+
* Takes a hold for work no line of a surface runs (a joblet): it waits in
|
|
524
|
+
* the same lane as every line, and runs beside the lines it shares
|
|
525
|
+
* nothing with (#1273).
|
|
503
526
|
*
|
|
504
|
-
* @param
|
|
527
|
+
* @param holds - What the work changes, or 'all'.
|
|
528
|
+
* @returns Gives the hold back; a second call does nothing.
|
|
505
529
|
*/
|
|
506
|
-
|
|
530
|
+
hold_take(holds: string[] | 'all'): Promise<() => void>;
|
|
507
531
|
/**
|
|
508
|
-
* Waits on a person without holding the session: the request is sent
|
|
509
|
-
*
|
|
510
|
-
*
|
|
511
|
-
* and resumes with its own slot.
|
|
532
|
+
* Waits on a person without holding the session: the request is sent and
|
|
533
|
+
* what the line holds given up, so other lines run; when the person
|
|
534
|
+
* answers, the line queues for its turn again.
|
|
512
535
|
*
|
|
513
536
|
* @param ask - Sends the request (under the asking command's slot) and
|
|
514
537
|
* resolves with the person's answer.
|
|
@@ -621,6 +644,7 @@ export declare class CalypsoDaemon {
|
|
|
621
644
|
id: string;
|
|
622
645
|
kind: 'chell' | 'browser';
|
|
623
646
|
you: boolean;
|
|
647
|
+
asking?: string;
|
|
624
648
|
}>;
|
|
625
649
|
capabilities_current(): {
|
|
626
650
|
fileDelivery: boolean;
|
package/dist/daemon/server.js
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
*
|
|
25
25
|
* @module
|
|
26
26
|
*/
|
|
27
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
27
28
|
import { randomBytes } from 'node:crypto';
|
|
28
29
|
import { createServer } from 'node:http';
|
|
29
30
|
import { WebSocketServer, WebSocket } from 'ws';
|
|
@@ -32,7 +33,7 @@ import { token_matches } from './token.js';
|
|
|
32
33
|
import { RequestBroker } from './broker.js';
|
|
33
34
|
import { CONTRACT_VERSION } from '@fnndsc/menu';
|
|
34
35
|
import { clientMessage_parse, attach_parse } from '@fnndsc/menu';
|
|
35
|
-
import { Lane } from './lane.js';
|
|
36
|
+
import { Lane, LOCKS_ALL } from './lane.js';
|
|
36
37
|
import { claim_match, claimCandidates_list } from './claims.js';
|
|
37
38
|
import { DownloadLedger, downloadId_valid } from './downloads.js';
|
|
38
39
|
/**
|
|
@@ -62,8 +63,6 @@ const SCROLLBACK_DEFAULT = 200;
|
|
|
62
63
|
function surface_isBrowser(surface) {
|
|
63
64
|
return !surface.capabilities.shellCommands && surface.capabilities.claims.length > 0;
|
|
64
65
|
}
|
|
65
|
-
/** The slot while nobody holds the lane. */
|
|
66
|
-
const SLOT_EMPTY = { origin: null, id: null, instrument: false, line: null, startedAt: 0 };
|
|
67
66
|
/**
|
|
68
67
|
* A WebSocket daemon hosting one engine for attached surfaces, with a session
|
|
69
68
|
* bus broadcasting activity across them.
|
|
@@ -78,9 +77,6 @@ export class CalypsoDaemon {
|
|
|
78
77
|
/** The last executed command's measured facts, sticky across pushes. */
|
|
79
78
|
lastCommand;
|
|
80
79
|
telemetryProvider;
|
|
81
|
-
/** The line holding the lane, and since when; null while the lane is idle. */
|
|
82
|
-
currentLine = null;
|
|
83
|
-
currentStartedAt = 0;
|
|
84
80
|
telemetryTimer = null;
|
|
85
81
|
/** The index counts at the last heartbeat, to detect movement. */
|
|
86
82
|
telemetryLastIndex = null;
|
|
@@ -104,7 +100,7 @@ export class CalypsoDaemon {
|
|
|
104
100
|
}
|
|
105
101
|
/** Whether a foreground command is executing right now. */
|
|
106
102
|
busy_get() {
|
|
107
|
-
return this.
|
|
103
|
+
return [...this.running].some((slot) => slot.hold !== null);
|
|
108
104
|
}
|
|
109
105
|
webRoot;
|
|
110
106
|
wss = null;
|
|
@@ -122,22 +118,30 @@ export class CalypsoDaemon {
|
|
|
122
118
|
regard = null;
|
|
123
119
|
/** Unsubscribes from the engine's ambient events; null until started. */
|
|
124
120
|
ambientStop = null;
|
|
125
|
-
currentOrigin = null;
|
|
126
|
-
currentId = null;
|
|
127
121
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* question it invented, so an ask raised under one is refused rather
|
|
132
|
-
* than shown.
|
|
122
|
+
* The line the code runs inside: several may run at once, so each keeps
|
|
123
|
+
* its own slot, and whatever it raises (output, a question, a pipe
|
|
124
|
+
* segment) goes to its own surface.
|
|
133
125
|
*/
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
|
|
126
|
+
slotScope = new AsyncLocalStorage();
|
|
127
|
+
/** The lines running now, waiting aside or not, in the order they started. */
|
|
128
|
+
running = new Set();
|
|
129
|
+
/** Names each line for the engine. */
|
|
130
|
+
lineCount = 0;
|
|
137
131
|
/** Surfaces whose STOP found their command still waiting its turn, and when. */
|
|
138
132
|
stopPending = new Map();
|
|
139
|
-
/**
|
|
140
|
-
|
|
133
|
+
/** The surface of the line the code runs inside, or null outside any. */
|
|
134
|
+
get currentOrigin() {
|
|
135
|
+
return this.slotScope.getStore()?.origin ?? null;
|
|
136
|
+
}
|
|
137
|
+
/** The surface's id for the line the code runs inside, or null outside any. */
|
|
138
|
+
get currentId() {
|
|
139
|
+
return this.slotScope.getStore()?.id ?? null;
|
|
140
|
+
}
|
|
141
|
+
/** Whether the line the code runs inside is surface-internal. */
|
|
142
|
+
get currentInstrument() {
|
|
143
|
+
return this.slotScope.getStore()?.instrument === true;
|
|
144
|
+
}
|
|
141
145
|
// One RequestBroker per surface-delegated request kind. The broker owns the
|
|
142
146
|
// whole correlation lifecycle uniformly: id generation, close-guard,
|
|
143
147
|
// origin-validated settles, listener cleanup. See broker.ts.
|
|
@@ -213,13 +217,16 @@ export class CalypsoDaemon {
|
|
|
213
217
|
const index = { jobs: snapshot.jobs, feeds: snapshot.feeds };
|
|
214
218
|
// The lane rides the heartbeat: which command holds it, for how long,
|
|
215
219
|
// and how many wait behind it. A surface reads it as a gauge.
|
|
220
|
+
const working = [...this.running].find((slot) => slot.hold !== null && !slot.instrument);
|
|
216
221
|
const lane = {
|
|
217
|
-
running:
|
|
218
|
-
? { line:
|
|
222
|
+
running: working !== undefined
|
|
223
|
+
? { line: working.line, sinceMs: Math.round(performance.now() - working.startedAt), surface: working.origin.id }
|
|
219
224
|
: null,
|
|
220
225
|
waiting: this.lane.waiting,
|
|
221
226
|
// Questions open: each one's command has stepped out of the lane.
|
|
222
227
|
questions: [...this.questionsOpen].map(([surface, open]) => ({ surface: surface.id, message: open.message, sinceMs: Math.round(performance.now() - open.at) })),
|
|
228
|
+
// The joblets running: a restart asked from any surface says it ends them.
|
|
229
|
+
joblets: this.engine.joblets_running?.() ?? [],
|
|
223
230
|
};
|
|
224
231
|
// Who else is here: a restart asked from one surface names the rest.
|
|
225
232
|
// A surface that runs host shells is a terminal; one that does not, a browser.
|
|
@@ -278,6 +285,10 @@ export class CalypsoDaemon {
|
|
|
278
285
|
this.backend_serve(request, response);
|
|
279
286
|
return;
|
|
280
287
|
}
|
|
288
|
+
if (requestPath === '/joblets') {
|
|
289
|
+
this.joblets_serve(request, response);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
281
292
|
if (this.webRoot !== undefined) {
|
|
282
293
|
staticRequest_handle(this.webRoot, request, response);
|
|
283
294
|
return;
|
|
@@ -348,6 +359,25 @@ export class CalypsoDaemon {
|
|
|
348
359
|
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
349
360
|
response.end(JSON.stringify({ backend: this.backend ?? null }));
|
|
350
361
|
}
|
|
362
|
+
/**
|
|
363
|
+
* Says which joblets run now (`line &`): `{"joblets": [{number, line,
|
|
364
|
+
* waiting}]}`. A host about to restart the session (porter, for a new
|
|
365
|
+
* release) asks first, since a restart ends them (#1273). Gated by the
|
|
366
|
+
* attach token, as `/backend` is.
|
|
367
|
+
*
|
|
368
|
+
* @param request - The incoming HTTP request.
|
|
369
|
+
* @param response - The response to write.
|
|
370
|
+
*/
|
|
371
|
+
joblets_serve(request, response) {
|
|
372
|
+
const query = new URL(request.url ?? '/', 'http://localhost').searchParams;
|
|
373
|
+
if (!token_matches(this.token, query.get('token') ?? '')) {
|
|
374
|
+
response.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });
|
|
375
|
+
response.end('not found');
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
379
|
+
response.end(JSON.stringify({ joblets: this.engine.joblets_running?.() ?? [] }));
|
|
380
|
+
}
|
|
351
381
|
/**
|
|
352
382
|
* Serves one ChRIS file's bytes over HTTP: `GET /vfs?path=...&token=...`.
|
|
353
383
|
*
|
|
@@ -624,7 +654,7 @@ export class CalypsoDaemon {
|
|
|
624
654
|
// One lane for every surface: commands take turns, and a command
|
|
625
655
|
// waiting on a person stands aside (person_await), so a pane's own
|
|
626
656
|
// reads, or another surface's command, run while a question is open.
|
|
627
|
-
void this.
|
|
657
|
+
void this.line_run(attached, value);
|
|
628
658
|
}
|
|
629
659
|
else if (value.type === 'cancel') {
|
|
630
660
|
this.cancel_run(attached, value);
|
|
@@ -719,8 +749,10 @@ export class CalypsoDaemon {
|
|
|
719
749
|
// SIGHUP does: one still running is stopped, or nothing could stop it.
|
|
720
750
|
// A browser is not a shell (it reloads and reattaches), and its
|
|
721
751
|
// command runs on (a chell declares a filesystem; a browser does not).
|
|
722
|
-
if (surface &&
|
|
723
|
-
this.
|
|
752
|
+
if (surface && (surface.capabilities.localFilesystem || surface.capabilities.terminal !== undefined)) {
|
|
753
|
+
for (const slot of this.running)
|
|
754
|
+
if (slot.origin === surface && slot.hold !== null)
|
|
755
|
+
this.command_stop(slot);
|
|
724
756
|
}
|
|
725
757
|
if (surface) {
|
|
726
758
|
this.surfaces.delete(surface);
|
|
@@ -730,6 +762,7 @@ export class CalypsoDaemon {
|
|
|
730
762
|
// A detached surface holds no watches: whatever it kept live is
|
|
731
763
|
// released, and a subject nobody else watches stops being sampled.
|
|
732
764
|
this.engine.watch_release?.(surface.id);
|
|
765
|
+
this.engine.origin_forget?.(surface.id);
|
|
733
766
|
}
|
|
734
767
|
});
|
|
735
768
|
}
|
|
@@ -852,12 +885,13 @@ export class CalypsoDaemon {
|
|
|
852
885
|
* @returns Nothing. The running command reports its normal final envelope.
|
|
853
886
|
*/
|
|
854
887
|
cancel_run(origin, message) {
|
|
855
|
-
|
|
888
|
+
const slot = [...this.running].find((each) => each.origin === origin && each.id === message.id && each.hold !== null);
|
|
889
|
+
if (slot === undefined) {
|
|
856
890
|
this.send(origin.socket, { type: 'error', reason: 'cancel: no matching foreground command' });
|
|
857
891
|
return;
|
|
858
892
|
}
|
|
859
893
|
// Ctrl-C is a stop: the command is asked to end, and abandoned if it does not.
|
|
860
|
-
this.command_stop();
|
|
894
|
+
this.command_stop(slot);
|
|
861
895
|
}
|
|
862
896
|
/**
|
|
863
897
|
* The operator's STOP: ends the command running for this surface, its own
|
|
@@ -867,18 +901,21 @@ export class CalypsoDaemon {
|
|
|
867
901
|
* @param _message - The request.
|
|
868
902
|
*/
|
|
869
903
|
stop_run(origin, _message) {
|
|
870
|
-
const running = this.currentOrigin;
|
|
871
904
|
// A surface's silent housekeeping is not what the operator stops. A line
|
|
872
905
|
// whose surface has gone is anyone's to stop: nobody else can.
|
|
873
|
-
|
|
874
|
-
|
|
906
|
+
const stoppable = (slot) => !slot.instrument && (this.stopper_resolve(slot.origin) === origin || !this.surfaces.has(slot.origin));
|
|
907
|
+
const working = [...this.running].filter((slot) => slot.hold !== null && stoppable(slot));
|
|
908
|
+
if (working.length > 0) {
|
|
909
|
+
for (const slot of working)
|
|
910
|
+
this.command_stop(slot);
|
|
875
911
|
return;
|
|
876
912
|
}
|
|
877
|
-
// Its command may be waiting aside on a task: stopped where it waits
|
|
878
|
-
|
|
913
|
+
// Its command may be waiting aside on a task: stopped where it waits,
|
|
914
|
+
// its own surface's first, else one whose surface has gone.
|
|
915
|
+
const asides = [...this.running].filter((slot) => slot.hold === null && slot.asideStoppable && stoppable(slot));
|
|
916
|
+
const aside = asides.find((slot) => this.stopper_resolve(slot.origin) === origin) ?? asides[0];
|
|
879
917
|
if (aside !== undefined) {
|
|
880
|
-
this.engine.
|
|
881
|
-
this.asideTurns.delete(aside);
|
|
918
|
+
this.engine.line_abort?.(aside.key);
|
|
882
919
|
return;
|
|
883
920
|
}
|
|
884
921
|
// Its command may be waiting its turn behind someone else's: it is
|
|
@@ -906,12 +943,11 @@ export class CalypsoDaemon {
|
|
|
906
943
|
* after {@link STOP_GRACE_MS} is abandoned, so the session is free again
|
|
907
944
|
* whatever the command was waiting on.
|
|
908
945
|
*/
|
|
909
|
-
command_stop() {
|
|
910
|
-
|
|
911
|
-
this.engine.line_abort?.() ?? this.engine.line_cancel?.();
|
|
946
|
+
command_stop(slot) {
|
|
947
|
+
this.engine.line_abort?.(slot.key) ?? this.engine.line_cancel?.(slot.key);
|
|
912
948
|
const timer = setTimeout(() => {
|
|
913
|
-
if (this.
|
|
914
|
-
|
|
949
|
+
if (this.running.has(slot))
|
|
950
|
+
slot.abandon?.();
|
|
915
951
|
}, STOP_GRACE_MS);
|
|
916
952
|
timer.unref?.();
|
|
917
953
|
}
|
|
@@ -981,13 +1017,6 @@ export class CalypsoDaemon {
|
|
|
981
1017
|
void this.promptline_push(surface);
|
|
982
1018
|
// Regard is a retained cell: a late attacher receives the current value
|
|
983
1019
|
// rather than waiting for the operator's next indication.
|
|
984
|
-
// The numbering is retained the same way: a surface that attaches after
|
|
985
|
-
// a listing still knows which rows `@2` would reach, so its index pills
|
|
986
|
-
// light on the right pane rather than nowhere.
|
|
987
|
-
const numbering = this.engine.numbering_get?.() ?? null;
|
|
988
|
-
if (numbering !== null) {
|
|
989
|
-
this.send(socket, { type: 'numbered', id: numbering.id, source: numbering.source, rows: numbering.rows, handles: numbering.handles });
|
|
990
|
-
}
|
|
991
1020
|
const retained = this.regard ?? this.engine.regard_get?.() ?? null;
|
|
992
1021
|
if (retained !== null) {
|
|
993
1022
|
this.send(socket, { type: 'regard', regard: retained });
|
|
@@ -1018,20 +1047,34 @@ export class CalypsoDaemon {
|
|
|
1018
1047
|
/**
|
|
1019
1048
|
* Relays an engine-originated event to every surface: a sampler's refreshed
|
|
1020
1049
|
* model rides the session bus tagged with the daemon as its surface (off
|
|
1021
|
-
* the scrollback — it is state, not something the operator did),
|
|
1022
|
-
* watch state change goes out as its own message
|
|
1050
|
+
* the scrollback — it is state, not something the operator did), a
|
|
1051
|
+
* watch state change goes out as its own message, and a numbering goes
|
|
1052
|
+
* only to the surface whose line listed.
|
|
1023
1053
|
*
|
|
1024
1054
|
* @param event - The ambient event.
|
|
1025
1055
|
*/
|
|
1026
1056
|
ambient_relay(event) {
|
|
1027
1057
|
for (const surface of this.surfaces) {
|
|
1028
1058
|
if (event.kind === 'envelope') {
|
|
1029
|
-
|
|
1059
|
+
// An envelope for one surface (a joblet's notice) goes to it while it
|
|
1060
|
+
// is attached, and to everyone once it has gone.
|
|
1061
|
+
const mine = event.origin === undefined
|
|
1062
|
+
|| ![...this.surfaces].some((each) => this.stopper_resolve(each).id === event.origin)
|
|
1063
|
+
|| this.stopper_resolve(surface).id === event.origin;
|
|
1064
|
+
if (mine)
|
|
1065
|
+
this.send(surface.socket, { type: 'session', surface: DAEMON_SURFACE, envelope: event.envelope });
|
|
1066
|
+
}
|
|
1067
|
+
else if (event.kind === 'progress') {
|
|
1068
|
+
// A joblet's progress is its own surface's, live: nobody else's views follow it.
|
|
1069
|
+
if (this.stopper_resolve(surface).id === event.origin)
|
|
1070
|
+
this.send(surface.socket, { type: 'progress', id: event.id, ...event.progress });
|
|
1030
1071
|
}
|
|
1031
1072
|
else if (event.kind === 'watched') {
|
|
1032
1073
|
this.send(surface.socket, { type: 'watched', subject: event.subject, state: event.state });
|
|
1033
1074
|
}
|
|
1034
|
-
else {
|
|
1075
|
+
else if (this.stopper_resolve(surface).id === event.origin) {
|
|
1076
|
+
// A numbering is the listing surface's own: another surface's `@3`
|
|
1077
|
+
// still counts the rows it read (#1273).
|
|
1035
1078
|
this.send(surface.socket, { type: 'numbered', id: event.id, source: event.source, rows: event.rows, handles: event.handles });
|
|
1036
1079
|
}
|
|
1037
1080
|
}
|
|
@@ -1108,27 +1151,74 @@ export class CalypsoDaemon {
|
|
|
1108
1151
|
}
|
|
1109
1152
|
}
|
|
1110
1153
|
}
|
|
1154
|
+
/**
|
|
1155
|
+
* What a line holds while it runs, as the engine reads it off the line: a
|
|
1156
|
+
* read runs beside anything, and lines changing the same piece of the
|
|
1157
|
+
* session take turns (#1273).
|
|
1158
|
+
*
|
|
1159
|
+
* @param line - The line.
|
|
1160
|
+
* @returns What it holds.
|
|
1161
|
+
*/
|
|
1162
|
+
lineWant_get(line) {
|
|
1163
|
+
let held = 'all';
|
|
1164
|
+
try {
|
|
1165
|
+
held = this.engine.line_holds?.(line) ?? 'all';
|
|
1166
|
+
}
|
|
1167
|
+
catch {
|
|
1168
|
+
// A line the engine cannot read runs alone, as every line once did.
|
|
1169
|
+
}
|
|
1170
|
+
return held === 'all' ? LOCKS_ALL : new Set(held);
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Runs one line in its turn: it waits until nothing running holds what it
|
|
1174
|
+
* changes, then runs in a slot of its own.
|
|
1175
|
+
*
|
|
1176
|
+
* @param origin - The surface that submitted the request.
|
|
1177
|
+
* @param message - The execute request.
|
|
1178
|
+
*/
|
|
1179
|
+
async line_run(origin, message) {
|
|
1180
|
+
this.lineCount += 1;
|
|
1181
|
+
const slot = {
|
|
1182
|
+
key: `line-${this.lineCount}`,
|
|
1183
|
+
origin,
|
|
1184
|
+
id: message.id,
|
|
1185
|
+
instrument: message.instrument === true,
|
|
1186
|
+
line: message.line,
|
|
1187
|
+
startedAt: performance.now(),
|
|
1188
|
+
want: this.lineWant_get(message.line),
|
|
1189
|
+
hold: null,
|
|
1190
|
+
asideStoppable: false,
|
|
1191
|
+
abandon: null,
|
|
1192
|
+
};
|
|
1193
|
+
slot.hold = await this.lane.acquire(slot.want);
|
|
1194
|
+
slot.startedAt = performance.now();
|
|
1195
|
+
this.running.add(slot);
|
|
1196
|
+
try {
|
|
1197
|
+
// The command runs inside its own slot, so whatever the engine raises
|
|
1198
|
+
// (a prompt, output, a pipe segment) reaches the surface that
|
|
1199
|
+
// submitted it, whatever else runs meanwhile.
|
|
1200
|
+
await this.slotScope.run(slot, () => this.execute_run(slot, message));
|
|
1201
|
+
}
|
|
1202
|
+
finally {
|
|
1203
|
+
this.running.delete(slot);
|
|
1204
|
+
if (slot.hold !== null)
|
|
1205
|
+
this.lane.release(slot.hold);
|
|
1206
|
+
slot.hold = null;
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1111
1209
|
/**
|
|
1112
1210
|
* Runs one execute request: replies to the requester with the result, and
|
|
1113
1211
|
* publishes each envelope to the session bus.
|
|
1114
1212
|
*
|
|
1115
|
-
* @param
|
|
1213
|
+
* @param slot - The line's slot, holding its turn.
|
|
1116
1214
|
* @param message - The execute request.
|
|
1117
1215
|
*/
|
|
1118
|
-
async execute_run(
|
|
1119
|
-
|
|
1120
|
-
// the engine raises is asked of the surface that submitted the command.
|
|
1121
|
-
// The slot is the lane holder's: set when the command takes its turn,
|
|
1122
|
-
// put aside while it waits on a person, and cleared when it finishes.
|
|
1123
|
-
this.currentOrigin = origin;
|
|
1124
|
-
this.currentId = message.id;
|
|
1125
|
-
this.currentInstrument = message.instrument === true;
|
|
1216
|
+
async execute_run(slot, message) {
|
|
1217
|
+
const origin = slot.origin;
|
|
1126
1218
|
let envelopes;
|
|
1127
1219
|
let failureReason;
|
|
1128
|
-
const startedAt =
|
|
1129
|
-
|
|
1130
|
-
this.currentStartedAt = startedAt;
|
|
1131
|
-
try {
|
|
1220
|
+
const startedAt = slot.startedAt;
|
|
1221
|
+
{
|
|
1132
1222
|
// A STOP pressed while this command waited its turn ends it unrun.
|
|
1133
1223
|
const stopper = this.stopper_resolve(origin);
|
|
1134
1224
|
const pendingAt = message.instrument === true ? undefined : this.stopPending.get(stopper);
|
|
@@ -1142,15 +1232,17 @@ export class CalypsoDaemon {
|
|
|
1142
1232
|
// A stopped command that does not end is abandoned: the session
|
|
1143
1233
|
// stops waiting, and whatever it does later is no one's.
|
|
1144
1234
|
const abandoned = new Promise((resolve) => {
|
|
1145
|
-
|
|
1235
|
+
slot.abandon = () => resolve([{ status: 'error', rendered: '', renderedErr: `stopped: \`${message.line}\` did not end when asked and was abandoned\n` }]);
|
|
1146
1236
|
});
|
|
1147
|
-
|
|
1237
|
+
// The line counts against its surface's own last answer: a terminal
|
|
1238
|
+
// a surface shows is that surface (#1273).
|
|
1239
|
+
envelopes = await Promise.race([this.engine.line_execute(message.line, { origin: stopper.id, id: slot.key }), abandoned]);
|
|
1148
1240
|
}
|
|
1149
1241
|
catch (err) {
|
|
1150
1242
|
failureReason = err instanceof Error ? err.message : String(err);
|
|
1151
1243
|
}
|
|
1152
1244
|
finally {
|
|
1153
|
-
|
|
1245
|
+
slot.abandon = null;
|
|
1154
1246
|
}
|
|
1155
1247
|
this.lastCommand = {
|
|
1156
1248
|
durationMs: Math.round(performance.now() - startedAt),
|
|
@@ -1183,35 +1275,29 @@ export class CalypsoDaemon {
|
|
|
1183
1275
|
});
|
|
1184
1276
|
}
|
|
1185
1277
|
}
|
|
1186
|
-
finally {
|
|
1187
|
-
this.slot_restore(SLOT_EMPTY);
|
|
1188
|
-
}
|
|
1189
1278
|
}
|
|
1190
1279
|
/**
|
|
1191
|
-
*
|
|
1280
|
+
* Takes a hold for work no line of a surface runs (a joblet): it waits in
|
|
1281
|
+
* the same lane as every line, and runs beside the lines it shares
|
|
1282
|
+
* nothing with (#1273).
|
|
1192
1283
|
*
|
|
1193
|
-
* @
|
|
1284
|
+
* @param holds - What the work changes, or 'all'.
|
|
1285
|
+
* @returns Gives the hold back; a second call does nothing.
|
|
1194
1286
|
*/
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
this.currentOrigin = slot.origin;
|
|
1205
|
-
this.currentId = slot.id;
|
|
1206
|
-
this.currentInstrument = slot.instrument;
|
|
1207
|
-
this.currentLine = slot.line;
|
|
1208
|
-
this.currentStartedAt = slot.startedAt;
|
|
1287
|
+
async hold_take(holds) {
|
|
1288
|
+
const hold = await this.lane.acquire(holds === 'all' ? LOCKS_ALL : new Set(holds));
|
|
1289
|
+
let given = false;
|
|
1290
|
+
return () => {
|
|
1291
|
+
if (given)
|
|
1292
|
+
return;
|
|
1293
|
+
given = true;
|
|
1294
|
+
this.lane.release(hold);
|
|
1295
|
+
};
|
|
1209
1296
|
}
|
|
1210
1297
|
/**
|
|
1211
|
-
* Waits on a person without holding the session: the request is sent
|
|
1212
|
-
*
|
|
1213
|
-
*
|
|
1214
|
-
* and resumes with its own slot.
|
|
1298
|
+
* Waits on a person without holding the session: the request is sent and
|
|
1299
|
+
* what the line holds given up, so other lines run; when the person
|
|
1300
|
+
* answers, the line queues for its turn again.
|
|
1215
1301
|
*
|
|
1216
1302
|
* @param ask - Sends the request (under the asking command's slot) and
|
|
1217
1303
|
* resolves with the person's answer.
|
|
@@ -1219,27 +1305,22 @@ export class CalypsoDaemon {
|
|
|
1219
1305
|
*/
|
|
1220
1306
|
async person_await(ask, stoppable = false) {
|
|
1221
1307
|
const answered = ask();
|
|
1222
|
-
const mine = this.
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
//
|
|
1226
|
-
|
|
1227
|
-
//
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
this.lane.release();
|
|
1308
|
+
const mine = this.slotScope.getStore();
|
|
1309
|
+
if (mine === undefined || mine.hold === null)
|
|
1310
|
+
return await answered;
|
|
1311
|
+
// What the line holds is given up while it waits, and its slot stays
|
|
1312
|
+
// its own: the engine keeps its per-line state (its exit code, a pipe's
|
|
1313
|
+
// untaken input, its cancellation) per line, so nothing is handed over.
|
|
1314
|
+
// A line waiting aside on a task stays the operator's to stop.
|
|
1315
|
+
mine.asideStoppable = stoppable && !mine.instrument;
|
|
1316
|
+
this.lane.release(mine.hold);
|
|
1317
|
+
mine.hold = null;
|
|
1233
1318
|
try {
|
|
1234
1319
|
return await answered;
|
|
1235
1320
|
}
|
|
1236
1321
|
finally {
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
await this.lane.acquire();
|
|
1240
|
-
this.slot_restore(mine);
|
|
1241
|
-
if (engineTurn !== undefined)
|
|
1242
|
-
this.engine.turn_resume?.(engineTurn);
|
|
1322
|
+
mine.asideStoppable = false;
|
|
1323
|
+
mine.hold = await this.lane.acquire(mine.want);
|
|
1243
1324
|
}
|
|
1244
1325
|
}
|
|
1245
1326
|
/**
|