-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-context.ts
More file actions
490 lines (450 loc) · 15.2 KB
/
session-context.ts
File metadata and controls
490 lines (450 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import type { EventSeq, JobId } from "@arcp/core";
import {
type BaseEnvelope,
buildEnvelope,
RoundTripEnvelopeSchema,
} from "@arcp/core/envelope";
import {
ARCPError,
HeartbeatLostError,
InternalError,
InvalidRequestError,
} from "@arcp/core/errors";
import { classifyUnknownType, CORE_MESSAGE_TYPES } from "@arcp/core/extensions";
import type { Logger } from "@arcp/core/logger";
import {
type Envelope,
EnvelopeSchema,
type JobErrorPayload,
} from "@arcp/core/messages";
import { PendingRegistry, SessionState } from "@arcp/core/state";
import type { Transport, WireFrame } from "@arcp/core/transport";
import { newMessageId } from "@arcp/core/util";
import { Either, Schema } from "effect";
import { JobManager } from "./job.js";
import type { ARCPServer } from "./server.js";
import type { EventSeqSource, Handler } from "./types.js";
const decodeRoundTripEnvelope = Schema.decodeUnknownSync(
RoundTripEnvelopeSchema,
);
const decodeEnvelope = Schema.decodeUnknownEither(EnvelopeSchema);
const KNOWN_CORE_TYPES: ReadonlySet<string> = new Set(CORE_MESSAGE_TYPES);
const HANDSHAKE_TYPES = new Set<string>(["session.hello"]);
// session.ping/pong/ack are session-control envelopes (not event-seq-bearing),
// so they bypass the inbound dedupe-and-log step.
const INBOUND_DEDUPE_SKIP: ReadonlySet<string> = new Set([
"session.ping",
"session.pong",
"session.ack",
]);
const DEFAULT_MAX_BUFFERED_EVENTS = 10_000;
const DEFAULT_MAX_BUFFERED_BYTES = 16 * 1024 * 1024; // 16 MiB
const DEFAULT_BACK_PRESSURE_THRESHOLD = 1000;
/**
* Per-transport session context. Drives the handshake and dispatches
* inbound envelopes.
*/
export class SessionContext implements EventSeqSource {
public readonly state = new SessionState();
public readonly jobs = new JobManager();
public readonly pending = new PendingRegistry();
public logger: Logger;
private readonly handlers = new Map<string, Handler>();
private closed = false;
private eventSeq = 0;
private bufferedEventCount = 0;
private bufferedBytes = 0;
private lastMessageAt: number = Date.now();
private lastInboundAt: number = Date.now();
/** Active idempotent keys for jobs that resolved through this session. */
private readonly localKeys = new Set<string>();
/** v1.1 §6.2 — negotiated feature set. */
private _negotiatedFeatures: readonly string[] = [];
/** v1.1 §6.4 — periodic ping timer. */
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
/** v1.1 §6.4 — pending ping nonce awaiting pong. */
private outstandingPingNonce: string | null = null;
/** v1.1 §6.5 — highest seq the client has acknowledged. */
private lastAckedSeq = 0;
private backPressureNotified = false;
/**
* v1.1 §7.6 — jobs we are observing as a subscriber (not the submitter).
* Maps job_id → unsubscribe callback.
*/
public readonly subscriptions = new Map<string, () => void>();
public constructor(
public readonly transport: Transport,
public readonly server: ARCPServer,
logger: Logger,
) {
this.logger = logger;
}
public registerHandler(type: string, handler: Handler): void {
this.handlers.set(type, handler);
}
public nextEventSeq(): EventSeq {
this.eventSeq += 1;
return this.eventSeq;
}
public get latestEventSeq(): EventSeq {
return this.eventSeq;
}
public setEventSeq(value: number): void {
this.eventSeq = value;
}
public touch(): void {
this.lastMessageAt = Date.now();
}
public get lastActivityAt(): number {
return this.lastMessageAt;
}
public get lastInboundActivityAt(): number {
return this.lastInboundAt;
}
public addLocalIdempotencyKey(key: string): void {
this.localKeys.add(key);
}
public hasLocalIdempotencyKey(key: string): boolean {
return this.localKeys.has(key);
}
public get negotiatedFeatures(): readonly string[] {
return this._negotiatedFeatures;
}
public hasFeature(name: string): boolean {
return this._negotiatedFeatures.includes(name);
}
public assignNegotiatedFeatures(features: readonly string[]): void {
this._negotiatedFeatures = features;
}
public get lastAckedEventSeq(): number {
return this.lastAckedSeq;
}
public recordAck(seq: number): void {
if (seq > this.lastAckedSeq) this.lastAckedSeq = seq;
}
/** Send an envelope through the transport. */
public async send(envelope: BaseEnvelope): Promise<void> {
if (this.closed || this.transport.closed) {
throw new InvalidRequestError("Cannot send: session closed");
}
this.touch();
await this.transport.send(envelope);
await this.persistOutbound(envelope);
await this.fanOutToSubscribers(envelope);
this.maybeEmitBackPressure();
}
private async persistOutbound(envelope: BaseEnvelope): Promise<void> {
if (envelope.session_id === undefined || envelope.session_id === "") return;
try {
await this.server.eventLog.append(envelope);
// Account against per-session caps for replay buffer estimation.
this.bufferedEventCount += 1;
this.bufferedBytes += JSON.stringify(envelope).length;
this.checkCaps();
} catch (error) {
this.logger.error({ err: error }, "event log append (outbound) failed");
}
}
private async fanOutToSubscribers(envelope: BaseEnvelope): Promise<void> {
if (envelope.job_id === undefined) return;
if (
envelope.type !== "job.event" &&
envelope.type !== "job.result" &&
envelope.type !== "job.error"
) {
return;
}
const subs = this.server.subscribers.get(envelope.job_id);
if (subs === undefined || subs.size === 0) return;
for (const sub of subs) {
if (sub === this || sub.state.id === undefined) continue;
await this.forwardEnvelopeToSubscriber(sub, envelope);
}
}
private async forwardEnvelopeToSubscriber(
sub: SessionContext,
envelope: BaseEnvelope,
): Promise<void> {
if (sub.state.id === undefined) return;
try {
const forwarded = buildEnvelope({
id: newMessageId(),
type: envelope.type,
payload: envelope.payload,
optional: {
session_id: sub.state.id,
job_id: envelope.job_id,
...(envelope.trace_id === undefined
? {}
: { trace_id: envelope.trace_id }),
...(envelope.event_seq === undefined
? {}
: { event_seq: sub.nextEventSeq() }),
},
});
await sub.transport.send(forwarded);
} catch {
// best-effort
}
}
private maybeEmitBackPressure(): void {
if (!this.hasFeature("ack")) return;
const lag = this.eventSeq - this.lastAckedSeq;
const threshold =
this.server.options.backPressureThreshold ??
DEFAULT_BACK_PRESSURE_THRESHOLD;
if (lag > threshold && !this.backPressureNotified) {
this.backPressureNotified = true;
// Best-effort emit — don't fail the outer send if this errors.
void this.emitBackPressureStatus(lag).catch(() => undefined);
return;
}
if (lag <= threshold / 2 && this.backPressureNotified) {
this.backPressureNotified = false;
}
}
private async emitBackPressureStatus(lag: number): Promise<void> {
if (this.closed || this.transport.closed) return;
if (this.state.id === undefined) return;
// Emit as a job.event with a synthetic, sessionless status body. We
// attach it to the most recently created job if any, else skip.
const live = this.jobs.list();
if (live.length === 0) return;
const job = live.at(-1);
if (job === undefined) return;
await job.emitEventKind("status", {
phase: "back_pressure",
message: `event_seq lag ${lag} exceeds threshold`,
});
}
private checkCaps(): void {
const caps = this.server.options.caps ?? {};
const maxEvents = caps.maxBufferedEvents ?? DEFAULT_MAX_BUFFERED_EVENTS;
const maxBytes = caps.maxBufferedBytes ?? DEFAULT_MAX_BUFFERED_BYTES;
if (this.bufferedEventCount > maxEvents || this.bufferedBytes > maxBytes) {
void this.emitSessionError(
new InternalError("Per-session buffered envelope cap exceeded", {
retryable: false,
}),
);
void this.terminate("session cap exceeded");
}
}
public async emitSessionError(error: ARCPError): Promise<void> {
if (this.closed || this.transport.closed) return;
try {
const envelope = buildEnvelope({
id: newMessageId(),
type: "session.error" as const,
payload: error.toPayload(),
...(this.state.id === undefined
? {}
: { optional: { session_id: this.state.id } }),
});
await this.transport.send(envelope);
} catch (sendError) {
this.logger.error(
{ err: sendError },
"failed to emit session.error envelope",
);
}
}
public async emitJobError(
jobId: JobId,
payload: JobErrorPayload,
): Promise<void> {
if (this.closed || this.transport.closed) return;
const sessionId = this.state.id;
if (sessionId === undefined) return;
try {
const envelope = buildEnvelope({
id: newMessageId(),
type: "job.error" as const,
payload,
optional: {
session_id: sessionId,
job_id: jobId,
event_seq: this.nextEventSeq(),
},
});
await this.send(envelope);
} catch (error) {
this.logger.error({ err: error }, "failed to emit job.error envelope");
}
}
/** Dispatch an inbound, raw frame. */
public async dispatchRaw(frame: WireFrame): Promise<void> {
this.touch();
this.lastInboundAt = Date.now();
const parsed = this.parseInboundFrame(frame);
if (parsed === null) return;
if (this.dropPreHandshakeNonHandshake(parsed)) return;
if (await this.dedupeInbound(parsed)) return;
const envelope = await this.validateInbound(parsed);
if (envelope === null) return;
await this.invokeHandler(envelope);
}
private parseInboundFrame(frame: WireFrame): BaseEnvelope | null {
try {
return decodeRoundTripEnvelope(frame);
} catch (error) {
this.logger.warn(
{ err: error },
"inbound envelope failed base-shape validation",
);
return null;
}
}
private dropPreHandshakeNonHandshake(parsed: BaseEnvelope): boolean {
if (this.state.isAccepted || HANDSHAKE_TYPES.has(parsed.type)) return false;
this.logger.warn(
{ type: parsed.type, id: parsed.id },
"dropping pre-handshake non-handshake message",
);
return true;
}
private async dedupeInbound(parsed: BaseEnvelope): Promise<boolean> {
// v1.1: session.ping/pong/ack are session-control (not event-seq-bearing),
// so we skip the dedupe-and-log step for them.
if (
this.state.id === undefined ||
parsed.session_id !== this.state.id ||
INBOUND_DEDUPE_SKIP.has(parsed.type)
) {
return false;
}
try {
const inserted = await this.server.eventLog.append(parsed);
if (inserted) return false;
this.logger.debug(
{ id: parsed.id },
"duplicate inbound, skipping dispatch",
);
return true;
} catch (error) {
this.logger.error({ err: error }, "event log append (inbound) failed");
return false;
}
}
private async validateInbound(
parsed: BaseEnvelope,
): Promise<Envelope | null> {
const result = decodeEnvelope(parsed);
if (Either.isRight(result)) return result.right;
// Mirror zod's `invalid_union_discriminator` behavior: if `type` is not a
// known core message type, treat as unknown type disposition rather than
// a generic schema failure.
if (!KNOWN_CORE_TYPES.has(parsed.type)) {
await this.handleUnknownTypeDisposition(parsed);
return null;
}
await this.emitSessionError(
new InvalidRequestError(`Invalid envelope: ${result.left.message}`),
);
return null;
}
private async handleUnknownTypeDisposition(
parsed: BaseEnvelope,
): Promise<void> {
const disposition = classifyUnknownType(parsed.type, {
extensionsObject: parsed.extensions,
});
if (disposition.kind === "drop") {
this.logger.debug({ type: parsed.type }, disposition.reason);
return;
}
await this.emitSessionError(
new InvalidRequestError(disposition.reason, {
details: { type: parsed.type },
}),
);
}
private async invokeHandler(envelope: Envelope): Promise<void> {
const handler = this.handlers.get(envelope.type);
if (handler === undefined) {
await this.emitSessionError(
new InvalidRequestError(`No handler registered for "${envelope.type}"`),
);
return;
}
try {
await handler(envelope, this);
} catch (error) {
this.logger.error({ err: error, type: envelope.type }, "handler threw");
const wrapped =
error instanceof ARCPError
? error
: new InternalError(
error instanceof Error ? error.message : String(error),
{ cause: error instanceof Error ? error : undefined },
);
// Best effort: route through session.error for now.
await this.emitSessionError(wrapped);
}
}
/** Start the v1.1 §6.4 heartbeat watchdog when the feature is negotiated. */
public startHeartbeat(): void {
if (!this.hasFeature("heartbeat")) return;
const intervalMs =
(this.server.options.heartbeatIntervalSeconds ?? 30) * 1000;
this.heartbeatTimer = setInterval(() => {
void this.heartbeatTick(intervalMs).catch(() => undefined);
}, intervalMs);
this.heartbeatTimer.unref();
}
private async heartbeatTick(intervalMs: number): Promise<void> {
if (this.closed || this.transport.closed) return;
// If we have an outstanding ping that's older than 2 * intervalMs, peer is
// dead. Emit HEARTBEAT_LOST as session.error and terminate.
const idleMs = Date.now() - this.lastInboundAt;
if (idleMs > 2 * intervalMs) {
await this.emitSessionError(
new HeartbeatLostError("No inbound activity within 2× heartbeat"),
);
await this.terminate("heartbeat lost");
return;
}
if (this.state.id === undefined) return;
const nonce = newMessageId();
this.outstandingPingNonce = nonce;
try {
await this.transport.send(
buildEnvelope({
id: newMessageId(),
type: "session.ping" as const,
payload: { nonce, sent_at: new Date().toISOString() },
optional: { session_id: this.state.id },
}),
);
} catch {
// best-effort
}
}
public handlePong(pingNonce: string): void {
if (this.outstandingPingNonce !== pingNonce) return;
this.outstandingPingNonce = null;
// pong itself counts as inbound activity for the idle check.
this.lastInboundAt = Date.now();
}
public async terminate(reason: string | undefined): Promise<void> {
if (this.closed) return;
this.closed = true;
if (this.heartbeatTimer !== null) {
clearInterval(this.heartbeatTimer);
this.heartbeatTimer = null;
}
this.jobs.cancelAll(reason ?? "session terminated");
this.pending.rejectAll(new InvalidRequestError("session terminated"));
// Drop subscriptions to other jobs.
for (const stop of this.subscriptions.values()) {
try {
stop();
} catch {
// best-effort
}
}
this.subscriptions.clear();
this.server.dropSession(this);
await this.transport.close(reason);
}
}