@sealant/sdk-next 0.40.0-next.980 → 0.40.0-next.981
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.
|
@@ -2,6 +2,11 @@ import { Effect, Redacted, type Scope } from "effect";
|
|
|
2
2
|
import { Socket } from "effect/socket";
|
|
3
3
|
import { SessionAttachError } from "../errors.js";
|
|
4
4
|
import type { AttachedSession } from "./service.js";
|
|
5
|
+
/**
|
|
6
|
+
* The most output records `output` holds unread: room for sealantd's whole retained journal (two
|
|
7
|
+
* 16 MiB segments) replayed in 4 KiB reads with nobody reading yet.
|
|
8
|
+
*/
|
|
9
|
+
export declare const MAX_QUEUED_RECORDS = 8192;
|
|
5
10
|
export interface AttachTarget {
|
|
6
11
|
readonly baseUrl: string;
|
|
7
12
|
readonly sessionId: string;
|
|
@@ -4,19 +4,34 @@
|
|
|
4
4
|
* `sessionAttachClientFrameSchema` in `@sealant/api-contracts`:
|
|
5
5
|
*
|
|
6
6
|
* server → client binary = one output record; text `seq` = the next binary frame's sequence,
|
|
7
|
+
* `skipped` = records the journal no longer holds were passed over,
|
|
7
8
|
* `error` = a client frame was refused, `end` = the session ended (then close)
|
|
8
9
|
* client → server binary = input; text `resize` | `signal`
|
|
9
10
|
*
|
|
10
11
|
* One `Socket` dials a new WebSocket for each connection, its URL read when it dials: every
|
|
11
12
|
* connection asks for the sequence after the last record delivered, so a socket closed without
|
|
12
|
-
* `end` is reopened where it stopped, and a record replayed twice is dropped.
|
|
13
|
-
*
|
|
13
|
+
* `end` is reopened where it stopped, and a record replayed twice is dropped. A record whose
|
|
14
|
+
* sequence is past the one asked for carries `skippedFrom`: the records between are gone. The
|
|
15
|
+
* socket's writer waits while it is reopening, so input sent across a cut is delivered once it is
|
|
16
|
+
* back. A close with 1008 (the credential was revoked or expired) is not reopened.
|
|
17
|
+
*
|
|
18
|
+
* `output` holds at most {@link MAX_QUEUED_RECORDS} unread records. The socket is read whether or
|
|
19
|
+
* not anyone reads `output`, so `end` always reaches `exit`; a reader that falls further behind
|
|
20
|
+
* gets an error on `output` instead of a process that grows without bound, and `exit`, `write`,
|
|
21
|
+
* `resize` and `signal` carry on.
|
|
14
22
|
*/
|
|
15
23
|
import { sessionAttachServerFrameSchema, } from "@sealant/api-contracts";
|
|
16
24
|
import { Deferred, Duration, Effect, Option, Queue, Random, Redacted, Ref, Schema, Stream, } from "effect";
|
|
17
25
|
import { Socket } from "effect/socket";
|
|
18
26
|
import { SessionAttachError, SessionEndedError } from "../errors.js";
|
|
19
27
|
const decodeServerFrame = Schema.decodeUnknownOption(Schema.fromJsonString(sessionAttachServerFrameSchema));
|
|
28
|
+
/**
|
|
29
|
+
* The most output records `output` holds unread: room for sealantd's whole retained journal (two
|
|
30
|
+
* 16 MiB segments) replayed in 4 KiB reads with nobody reading yet.
|
|
31
|
+
*/
|
|
32
|
+
export const MAX_QUEUED_RECORDS = 8192;
|
|
33
|
+
/** The close code the control plane ends a socket with once its credential no longer stands. */
|
|
34
|
+
const POLICY_VIOLATION = 1008;
|
|
20
35
|
/** Cuts in a row with no record received before the attachment gives up. */
|
|
21
36
|
const MAX_RECONNECTS = 8;
|
|
22
37
|
const RECONNECT_FIRST = Duration.millis(100);
|
|
@@ -32,15 +47,19 @@ const attachUrl = (target, from) => {
|
|
|
32
47
|
url.searchParams.set("ownerUserId", target.ownerUserId);
|
|
33
48
|
return url.toString();
|
|
34
49
|
};
|
|
35
|
-
const
|
|
50
|
+
const cutBy = (error) => error.reason instanceof Socket.SocketCloseError
|
|
51
|
+
? { kind: "cut", closeCode: error.reason.code, closeReason: error.reason.closeReason }
|
|
52
|
+
: { kind: "cut", closeCode: undefined, closeReason: undefined };
|
|
36
53
|
export const attachSession = (target) => Effect.gen(function* () {
|
|
37
54
|
const { sessionId } = target;
|
|
38
55
|
// The next sequence the consumer has not been given: what a reconnect asks for.
|
|
39
56
|
const next = yield* Ref.make(target.fromSequence);
|
|
40
57
|
const cuts = yield* Ref.make(0);
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
const output = yield* Queue.
|
|
58
|
+
// Bounded, and never waited on: a consumer that reads `exit` and not `output` must not keep
|
|
59
|
+
// the `end` frame from being read. A record that finds it full fails `output` instead.
|
|
60
|
+
const output = yield* Queue.dropping(MAX_QUEUED_RECORDS);
|
|
61
|
+
// Once `output` has failed for a reader that fell behind, records are no longer queued.
|
|
62
|
+
let overflowed = false;
|
|
44
63
|
const exit = yield* Deferred.make();
|
|
45
64
|
const opened = yield* Deferred.make();
|
|
46
65
|
// Closing the scope ends `output` for a consumer still reading it.
|
|
@@ -71,6 +90,9 @@ export const attachSession = (target) => Effect.gen(function* () {
|
|
|
71
90
|
const server = decoded.value;
|
|
72
91
|
if (server.t === "seq")
|
|
73
92
|
named = BigInt(server.sequence);
|
|
93
|
+
// The record after it comes with a `seq` past the one asked for, which says so.
|
|
94
|
+
else if (server.t === "skipped")
|
|
95
|
+
continue;
|
|
74
96
|
else if (server.t === "error") {
|
|
75
97
|
yield* Effect.logWarning("The control plane refused a frame on the attach socket").pipe(Effect.annotateLogs({ sessionId, message: server.message }));
|
|
76
98
|
}
|
|
@@ -87,12 +109,21 @@ export const attachSession = (target) => Effect.gen(function* () {
|
|
|
87
109
|
// Replayed after a reconnect but already delivered: once is enough.
|
|
88
110
|
if (sequence < wanted)
|
|
89
111
|
continue;
|
|
90
|
-
|
|
112
|
+
const chunk = sequence > wanted
|
|
113
|
+
? { sequence, data: frame, skippedFrom: wanted }
|
|
114
|
+
: { sequence, data: frame };
|
|
115
|
+
if (!overflowed && !(yield* Queue.offer(output, chunk))) {
|
|
116
|
+
overflowed = true;
|
|
117
|
+
yield* Queue.fail(output, new SessionAttachError({
|
|
118
|
+
sessionId,
|
|
119
|
+
message: `The reader of session ${sessionId}'s output fell more than ${String(MAX_QUEUED_RECORDS)} records behind; attach again from sequence ${String(wanted)}.`,
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
91
122
|
yield* Ref.set(next, sequence + 1n);
|
|
92
123
|
yield* Ref.set(cuts, 0);
|
|
93
124
|
}
|
|
94
125
|
}
|
|
95
|
-
})).pipe(Effect.catchTag("SocketError", (error) => Effect.succeed(
|
|
126
|
+
})).pipe(Effect.catchTag("SocketError", (error) => Effect.succeed(cutBy(error))));
|
|
96
127
|
const pump = Effect.gen(function* () {
|
|
97
128
|
for (;;) {
|
|
98
129
|
const ended = yield* connection;
|
|
@@ -111,6 +142,17 @@ export const attachSession = (target) => Effect.gen(function* () {
|
|
|
111
142
|
yield* failWith(error);
|
|
112
143
|
return;
|
|
113
144
|
}
|
|
145
|
+
if (ended.closeCode === POLICY_VIOLATION) {
|
|
146
|
+
// The credential was revoked or expired: reopening would be refused every time.
|
|
147
|
+
yield* failWith(new SessionAttachError({
|
|
148
|
+
sessionId,
|
|
149
|
+
message: `The control plane closed the attach socket for session ${sessionId}: ${ended.closeReason === undefined || ended.closeReason === ""
|
|
150
|
+
? "its credential no longer admits it"
|
|
151
|
+
: ended.closeReason}`,
|
|
152
|
+
closeCode: POLICY_VIOLATION,
|
|
153
|
+
}));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
114
156
|
const attempt = yield* Ref.getAndUpdate(cuts, (n) => n + 1);
|
|
115
157
|
if (attempt >= MAX_RECONNECTS) {
|
|
116
158
|
yield* failWith(new SessionAttachError({
|
|
@@ -93,20 +93,22 @@ export const make = Effect.gen(function* () {
|
|
|
93
93
|
path: `/v1/sessions/${encodeURIComponent(sessionId)}/output/stream`,
|
|
94
94
|
query: { fromSequence: from.toString(), ownerUserId },
|
|
95
95
|
})));
|
|
96
|
+
// A chunk past the one asked for says the records between are gone.
|
|
96
97
|
const deliver = (chunk) => Effect.gen(function* () {
|
|
98
|
+
const wanted = yield* Ref.get(next);
|
|
97
99
|
// Replayed after a reconnect but already delivered: once is enough.
|
|
98
|
-
if (chunk.sequence <
|
|
99
|
-
return
|
|
100
|
+
if (chunk.sequence < wanted)
|
|
101
|
+
return Option.none();
|
|
100
102
|
yield* Ref.set(next, chunk.sequence + 1n);
|
|
101
103
|
yield* Ref.set(cuts, 0);
|
|
102
|
-
return
|
|
104
|
+
return Option.some(chunk.sequence > wanted ? { ...chunk, skippedFrom: wanted } : chunk);
|
|
103
105
|
});
|
|
104
106
|
const following = connection.pipe(
|
|
105
107
|
// `end` is the only sign the session ended: a body that stops without it was cut.
|
|
106
108
|
Stream.takeUntil((event) => event.event === "end"), Stream.tap((event) => (event.event === "end" ? Ref.set(ended, true) : Effect.void)), Stream.filter((event) => event.event === "output" && event.id !== undefined), Stream.map((event) => ({
|
|
107
109
|
sequence: BigInt(event.id ?? "0"),
|
|
108
110
|
data: fromBase64(event.data),
|
|
109
|
-
})), Stream.
|
|
111
|
+
})), Stream.mapEffect(deliver), Stream.filter(Option.isSome), Stream.map((chunk) => chunk.value), Stream.catchTag("SealantTransportError", () => Stream.empty), Stream.concat(Stream.unwrap(Effect.map(Ref.get(ended), (done) => (done ? Stream.empty : reconnect)))));
|
|
110
112
|
const reconnect = Stream.unwrap(Effect.gen(function* () {
|
|
111
113
|
const attempt = yield* Ref.getAndUpdate(cuts, (n) => n + 1);
|
|
112
114
|
if (attempt >= OUTPUT_RECONNECTS) {
|
|
@@ -11,6 +11,13 @@ import type { SealantStreamRefusedError, SealantTransportError, SessionAttachErr
|
|
|
11
11
|
export interface SessionOutputChunk {
|
|
12
12
|
readonly sequence: bigint;
|
|
13
13
|
readonly data: Uint8Array;
|
|
14
|
+
/**
|
|
15
|
+
* Set when records before this one are gone: the session's output journal rotated away the
|
|
16
|
+
* records from `skippedFrom` up to before `sequence` before they could be sent (a
|
|
17
|
+
* `fromSequence` older than the journal, or a reader that fell behind it). Absent when this
|
|
18
|
+
* record follows the one before, or the sequence asked for.
|
|
19
|
+
*/
|
|
20
|
+
readonly skippedFrom?: bigint;
|
|
14
21
|
}
|
|
15
22
|
/** A POSIX signal number: 2 is `SIGINT`, 15 `SIGTERM`. */
|
|
16
23
|
export type SessionSignal = number;
|
|
@@ -40,7 +47,9 @@ export interface AttachedSession {
|
|
|
40
47
|
readonly sessionId: string;
|
|
41
48
|
/**
|
|
42
49
|
* Output records: the replay from `fromSequence`, then live, ending when the session ends. One
|
|
43
|
-
* consumer: each record is delivered once.
|
|
50
|
+
* consumer: each record is delivered once. It holds a bounded number of unread records and
|
|
51
|
+
* fails with `SessionAttachError` once its reader falls further behind; `exit` and input are
|
|
52
|
+
* not held up by a reader that is slow or gone.
|
|
44
53
|
*/
|
|
45
54
|
readonly output: Stream.Stream<SessionOutputChunk, SessionAttachError>;
|
|
46
55
|
/** Keystrokes for a `pty` session, stdin for a `pipe` one. A string is sent as UTF-8. */
|
|
@@ -51,7 +60,8 @@ export interface AttachedSession {
|
|
|
51
60
|
readonly signal: (signal: SessionSignal) => Effect.Effect<void, SessionEndedError | SessionAttachError>;
|
|
52
61
|
/**
|
|
53
62
|
* How the session ended, once the control plane says it did (its `end` frame). Fails with
|
|
54
|
-
* `SessionAttachError` when the socket could not be kept open
|
|
63
|
+
* `SessionAttachError` when the socket could not be kept open, at once (close code 1008) when
|
|
64
|
+
* the control plane closed it because the credential was revoked or expired.
|
|
55
65
|
*/
|
|
56
66
|
readonly exit: Effect.Effect<SessionEnded, SessionAttachError>;
|
|
57
67
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sealant/sdk-next",
|
|
3
|
-
"version": "0.40.0-next.
|
|
3
|
+
"version": "0.40.0-next.981",
|
|
4
4
|
"description": "The fluent public SDK for Sealant — create a workspace, run a harness, replay the record.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"node": ">=20"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@sealant/api-contracts": "npm:@sealant/api-contracts-next@0.40.0-next.
|
|
29
|
+
"@sealant/api-contracts": "npm:@sealant/api-contracts-next@0.40.0-next.981"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"effect": "^4.0.5"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "c0ba46649e62843667d107d9fc97093ab0afe92e",
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
}
|