-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
610 lines (573 loc) · 20.5 KB
/
client.ts
File metadata and controls
610 lines (573 loc) · 20.5 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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import { randomBytes } from "node:crypto";
import type { JobId, SessionId } from "@arcp/core";
import { type BaseEnvelope, buildEnvelope } from "@arcp/core/envelope";
import {
CancelledError,
InvalidRequestError,
UnauthenticatedError,
} from "@arcp/core/errors";
import { type Logger, rootLogger } from "@arcp/core/logger";
import type {
Capabilities,
Envelope,
JobAcceptedPayload,
JobListEntry,
JobResultPayload,
JobSubscribedPayload,
SessionJobsPayload,
SessionListJobsFilter,
SessionResume,
SessionWelcomePayload,
} from "@arcp/core/messages";
import { PendingRegistry, SessionState } from "@arcp/core/state";
import type { Transport, WireFrame } from "@arcp/core/transport";
import { Deferred, newMessageId } from "@arcp/core/util";
import { intersectFeatures, V1_1_FEATURES } from "@arcp/core/version";
import { dispatchEnvelope } from "./client-dispatch.js";
import {
buildByeEnvelope,
buildHelloEnvelope,
buildSubmitEnvelope,
buildSubscribeEnvelope,
buildUnsubscribeEnvelope,
} from "./client-envelopes.js";
import {
type InvocationState,
makeHandleFromInvocation,
} from "./client-handle.js";
import type {
ARCPClientOptions,
ClientHandler,
JobHandle,
JobSubscription,
SubmitOptions,
} from "./types.js";
// ARCP v1.1 client (additive over v1.0).
//
// Surface:
// - `connect(transport)` → handshake, returns SessionWelcomePayload.
// - `submit({ agent, input, ... })` → sends job.submit, awaits job.accepted.
// - `cancelJob(jobId, { reason? })` → sends job.cancel.
// - `close(reason?)` → sends session.bye then closes transport.
// - v1.1: `ack(seq)`, `listJobs(filter, opts)`, `subscribe(jobId, opts)`.
/**
* Client-side driver for an ARCP v1.1 session (§6).
*
* One client instance owns one transport and one session. After
* {@link ARCPClient.connect} resolves, the session is in the `accepted`
* phase and {@link ARCPClient.submit} may be called to launch jobs.
* Use {@link ARCPClient.resume} to recover a prior session within the
* runtime's resume window.
*/
export class ARCPClient {
public readonly state = new SessionState();
public readonly pending = new PendingRegistry();
public readonly logger: Logger;
private readonly handlers = new Map<string, ClientHandler>();
private transport: Transport | null = null;
private handshake: Deferred<SessionWelcomePayload> | null = null;
private readonly handshakeTimeoutMs: number;
/** Latest `event_seq` observed for this session. Used on resume. */
private lastEventSeq = 0;
/** Most recent welcome payload (carries the fresh `resume_token`). */
private welcome: SessionWelcomePayload | null = null;
/** In-flight submissions keyed by the originating envelope id (submit). */
private readonly invocationsByOriginId = new Map<string, InvocationState>();
/** Map job_id → invocation once `job.accepted` is received. */
private readonly invocationsByJobId = new Map<string, InvocationState>();
/** FIFO of in-flight submissions awaiting job.accepted (ordered binding). */
private readonly pendingAccepts: InvocationState[] = [];
/** v1.1 §6.2 — negotiated feature set (intersection with runtime). */
private _negotiatedFeatures: readonly string[] = [];
/** v1.1 §6.6 — pending list_jobs requests keyed by envelope id. */
private readonly pendingLists = new Map<
string,
Deferred<SessionJobsPayload>
>();
/** v1.1 §7.6 — pending subscribe requests keyed by job_id. */
private readonly pendingSubscribes = new Map<
string,
Deferred<JobSubscribedPayload>
>();
/** v1.1 §6.5 — auto-ack scheduler state. */
private readonly autoAckOpts: {
intervalMs: number;
minSeqDelta: number;
} | null = null;
private autoAckTimer: ReturnType<typeof setTimeout> | null = null;
private lastAckedSeq = 0;
public constructor(public readonly options: ARCPClientOptions) {
this.logger =
options.logger ?? rootLogger.child({ component: "arcp-client" });
this.handshakeTimeoutMs = options.handshakeTimeoutMs ?? 5000;
if (options.autoAck !== undefined && options.autoAck !== false) {
const o = options.autoAck === true ? {} : options.autoAck;
this.autoAckOpts = {
intervalMs: o.intervalMs ?? 250,
minSeqDelta: o.minSeqDelta ?? 32,
};
}
}
public get lastEventSeqObserved(): number {
return this.lastEventSeq;
}
public get welcomePayload(): SessionWelcomePayload | null {
return this.welcome;
}
/** v1.1 — features negotiated with the runtime (intersection). */
public get negotiatedFeatures(): readonly string[] {
return this._negotiatedFeatures;
}
/** v1.1 — check whether a feature is in the negotiated set. */
public hasFeature(name: string): boolean {
return this._negotiatedFeatures.includes(name);
}
/**
* Connect over `transport` and complete the handshake.
*
* Resolves with the negotiated `session.welcome` payload; rejects with
* an {@link ARCPError} on rejection, malformed envelopes, or timeout.
*/
public async connect(
transport: Transport,
opts: { signal?: AbortSignal } = {},
): Promise<SessionWelcomePayload> {
opts.signal?.throwIfAborted();
return this.connectInternal(transport, undefined, opts.signal);
}
/**
* Resume a prior session under the same principal. Sends a hello with the
* resume block and replays missed events upon welcome.
*/
public async resume(
transport: Transport,
resume: SessionResume,
opts: { signal?: AbortSignal } = {},
): Promise<SessionWelcomePayload> {
opts.signal?.throwIfAborted();
return this.connectInternal(transport, resume, opts.signal);
}
private async connectInternal(
transport: Transport,
resume: SessionResume | undefined,
signal?: AbortSignal,
): Promise<SessionWelcomePayload> {
if (this.transport !== null) {
throw new InvalidRequestError("ARCPClient is already connected");
}
this.transport = transport;
this.handshake = new Deferred<SessionWelcomePayload>();
this.wireTransport(transport);
const advertisedFeatures = this.options.features ?? V1_1_FEATURES;
const baseCaps = this.buildAdvertisedCapabilities(advertisedFeatures);
await transport.send(this.buildHelloEnvelope(baseCaps, resume));
return this.awaitHandshake(advertisedFeatures, signal);
}
private wireTransport(transport: Transport): void {
transport.onFrame((frame) => this.dispatchRaw(frame));
transport.onClose((err) => {
if (this.handshake !== null && !this.handshake.settled) {
this.handshake.reject(
new InvalidRequestError(
"Transport closed before handshake completed",
{ cause: err },
),
);
}
});
}
private buildAdvertisedCapabilities(
advertisedFeatures: readonly string[],
): Capabilities {
// v1.1 §6.2 — advertise features. If the consumer didn't supply a
// `capabilities` block, build one with our default features. If they
// did, augment with `features` (unless they explicitly set it).
const baseCaps: Capabilities = { ...this.options.capabilities };
if (baseCaps.features === undefined && advertisedFeatures.length > 0) {
baseCaps.features = [...advertisedFeatures];
}
baseCaps.encodings ??= ["json"];
return baseCaps;
}
private buildHelloEnvelope(
baseCaps: Capabilities,
resume: SessionResume | undefined,
): BaseEnvelope {
return buildHelloEnvelope({
id: newMessageId(),
options: this.options,
capabilities: baseCaps,
resume,
});
}
private async awaitHandshake(
advertisedFeatures: readonly string[],
signal: AbortSignal | undefined,
): Promise<SessionWelcomePayload> {
const timeout = setTimeout(() => {
if (this.handshake !== null && !this.handshake.settled) {
this.handshake.reject(new InvalidRequestError("Handshake timed out"));
}
}, this.handshakeTimeoutMs);
timeout.unref();
const onAbort = (): void => {
this.rejectHandshakeForAbort(signal);
};
signal?.addEventListener("abort", onAbort, { once: true });
const handshake = this.handshake;
if (handshake === null) {
throw new InvalidRequestError("Handshake state was cleared");
}
try {
const welcome = await handshake.promise;
this.welcome = welcome;
this.state.assignCapabilities(welcome.capabilities);
this.state.transition("accepted");
this._negotiatedFeatures = intersectFeatures(
advertisedFeatures,
welcome.capabilities.features,
);
return welcome;
} finally {
clearTimeout(timeout);
signal?.removeEventListener("abort", onAbort);
}
}
private rejectHandshakeForAbort(signal: AbortSignal | undefined): void {
if (this.handshake === null || this.handshake.settled) return;
const reason: unknown = signal?.reason;
this.handshake.reject(
new CancelledError("Handshake aborted by caller", {
cause: reason instanceof Error ? reason : undefined,
}),
);
}
/** Register a handler for a specific message type. */
public on(type: string, handler: ClientHandler): void {
this.handlers.set(type, handler);
}
/** Send an envelope to the runtime. Requires an accepted session. */
public async send(
env: BaseEnvelope,
opts: { signal?: AbortSignal } = {},
): Promise<void> {
opts.signal?.throwIfAborted();
if (this.transport === null)
throw new InvalidRequestError("Client not connected");
if (!this.state.isAccepted) {
throw new UnauthenticatedError("Cannot send: session not accepted");
}
await this.transport.send(env);
}
/** Close the underlying transport, optionally sending session.bye. */
public async close(reason?: string): Promise<void> {
this.rejectAllPending(new CancelledError("Client closing"));
this.clearAutoAckTimer();
if (this.transport === null) return;
await this.sendBye(reason);
await this.transport.close(reason);
this.transport = null;
}
private rejectAllPending(error: CancelledError): void {
this.pending.rejectAll(error);
for (const inv of this.invocationsByOriginId.values()) {
inv.acceptance.reject(error);
inv.completion.reject(error);
}
this.invocationsByOriginId.clear();
this.invocationsByJobId.clear();
this.pendingAccepts.length = 0;
for (const d of this.pendingLists.values()) d.reject(error);
this.pendingLists.clear();
for (const d of this.pendingSubscribes.values()) d.reject(error);
this.pendingSubscribes.clear();
}
private clearAutoAckTimer(): void {
if (this.autoAckTimer === null) return;
clearTimeout(this.autoAckTimer);
this.autoAckTimer = null;
}
private async sendBye(reason: string | undefined): Promise<void> {
if (this.transport === null) return;
const sessionId = this.state.id;
if (sessionId === undefined || !this.state.isAccepted) return;
try {
await this.transport.send(buildByeEnvelope(sessionId, reason));
} catch {
// best-effort
}
}
/**
* Submit a job. Returns once `job.accepted` arrives, with a handle that
* exposes `done` for the terminal `job.result` / `job.error`.
*/
public async submit(opts: SubmitOptions): Promise<JobHandle> {
if (this.transport === null) {
throw new InvalidRequestError("Client not connected");
}
if (!this.state.isAccepted) {
throw new UnauthenticatedError("Cannot submit: session not accepted");
}
const sessionId = this.state.id;
if (sessionId === undefined) {
throw new InvalidRequestError("session has no id");
}
const id = newMessageId();
const env = buildSubmitEnvelope({ id, sessionId, opts });
const invocation = this.registerSubmitInvocation(id, opts);
const abortHandled = this.wireSubmitAbort(invocation, opts.signal);
if (abortHandled === "aborted-before-submit") {
return makeHandleFromInvocation(invocation);
}
await this.transport.send(env);
// The routeJobEvent path resolves `acceptance` *and* registers the
// invocation in `invocationsByJobId` before this await returns, so any
// subsequent events arriving in the same tick will still route.
await invocation.acceptance.promise;
return makeHandleFromInvocation(invocation);
}
private registerSubmitInvocation(
id: string,
opts: SubmitOptions,
): InvocationState {
const invocation: InvocationState = {
jobId: null,
lease: null,
agent: undefined,
leaseConstraints: undefined,
budget: undefined,
traceId: opts.traceId,
events: [],
acceptance: new Deferred<JobAcceptedPayload>(),
completion: new Deferred<JobResultPayload>(),
chunks: new Map(),
};
// Mute unhandled-rejection on `completion` — callers consume it via
// `handle.done`. If the submit rejects pre-handle (e.g.
// AGENT_NOT_AVAILABLE before any `job.accepted`), this prevents a
// spurious unhandled promise rejection.
invocation.completion.promise.catch(() => undefined);
this.invocationsByOriginId.set(id, invocation);
this.pendingAccepts.push(invocation);
return invocation;
}
private wireSubmitAbort(
invocation: InvocationState,
signal: AbortSignal | undefined,
): "aborted-before-submit" | null {
if (signal === undefined) return null;
if (signal.aborted) {
invocation.acceptance.reject(new CancelledError("aborted before submit"));
return "aborted-before-submit";
}
const onAbort = (): void => {
if (invocation.jobId !== null) {
void this.cancelJob(invocation.jobId, {
reason: String(signal.reason ?? "abort"),
});
}
};
signal.addEventListener("abort", onAbort, { once: true });
return null;
}
/** Send a `job.cancel` envelope. */
public async cancelJob(
jobId: JobId,
options: { reason?: string; signal?: AbortSignal } = {},
): Promise<void> {
options.signal?.throwIfAborted();
if (this.transport === null)
throw new InvalidRequestError("Client not connected");
const sessionId = this.state.id;
if (sessionId === undefined)
throw new InvalidRequestError("session has no id");
const env = buildEnvelope({
id: newMessageId(),
type: "job.cancel" as const,
payload: options.reason === undefined ? {} : { reason: options.reason },
optional: { session_id: sessionId, job_id: jobId },
});
await this.transport.send(env);
}
/**
* v1.1 §6.5 — manually acknowledge that events with `event_seq ≤ seq` have
* been processed. The runtime MAY free buffered events earlier than the
* resume window. This is purely advisory and does not affect resume
* semantics.
*/
public async ack(
seq: number,
opts: { signal?: AbortSignal } = {},
): Promise<void> {
opts.signal?.throwIfAborted();
if (!this.hasFeature("ack")) {
throw new InvalidRequestError(
"session.ack requires the 'ack' feature to be negotiated",
);
}
if (this.transport === null)
throw new InvalidRequestError("Client not connected");
const sessionId = this.state.id;
if (sessionId === undefined)
throw new InvalidRequestError("session has no id");
const env = buildEnvelope({
id: newMessageId(),
type: "session.ack" as const,
payload: { last_processed_seq: seq },
optional: { session_id: sessionId },
});
await this.transport.send(env);
if (seq > this.lastAckedSeq) this.lastAckedSeq = seq;
}
/**
* v1.1 §6.6 — request a read-only inventory of jobs accessible in this
* session. Returns a single page; pass `cursor: result.nextCursor` to
* fetch additional pages.
*/
public async listJobs(
filter?: SessionListJobsFilter,
opts: { limit?: number; cursor?: string; signal?: AbortSignal } = {},
): Promise<{ jobs: JobListEntry[]; nextCursor: string | null }> {
opts.signal?.throwIfAborted();
if (!this.hasFeature("list_jobs")) {
throw new InvalidRequestError(
"session.list_jobs requires the 'list_jobs' feature to be negotiated",
);
}
if (this.transport === null)
throw new InvalidRequestError("Client not connected");
const sessionId = this.state.id;
if (sessionId === undefined)
throw new InvalidRequestError("session has no id");
const id = newMessageId();
const deferred = new Deferred<SessionJobsPayload>();
this.pendingLists.set(id, deferred);
const env = buildEnvelope({
id,
type: "session.list_jobs" as const,
payload: {
...(filter === undefined ? {} : { filter }),
...(opts.limit === undefined ? {} : { limit: opts.limit }),
...(opts.cursor === undefined ? {} : { cursor: opts.cursor }),
},
optional: { session_id: sessionId },
});
await this.transport.send(env);
const resp = await deferred.promise;
return { jobs: resp.jobs, nextCursor: resp.next_cursor };
}
/**
* v1.1 §7.6 — subscribe to a job by id, optionally replaying buffered
* history. The returned {@link JobSubscription} exposes `unsubscribe()`;
* events for the subscribed job arrive via the usual handlers
* (`client.on("job.event", ...)`) as they would for any in-session job.
*/
public async subscribe(
jobId: JobId,
opts: {
history?: boolean;
fromEventSeq?: number;
signal?: AbortSignal;
} = {},
): Promise<JobSubscription> {
opts.signal?.throwIfAborted();
if (!this.hasFeature("subscribe")) {
throw new InvalidRequestError(
"job.subscribe requires the 'subscribe' feature to be negotiated",
);
}
if (this.transport === null) {
throw new InvalidRequestError("Client not connected");
}
const sessionId = this.state.id;
if (sessionId === undefined) {
throw new InvalidRequestError("session has no id");
}
const deferred = new Deferred<JobSubscribedPayload>();
this.pendingSubscribes.set(jobId, deferred);
await this.transport.send(buildSubscribeEnvelope(jobId, sessionId, opts));
const ack = await deferred.promise;
return {
jobId,
subscribedFrom: ack.subscribed_from,
replayed: ack.replayed,
unsubscribe: () => this.unsubscribe(jobId, sessionId),
};
}
private async unsubscribe(jobId: JobId, sessionId: SessionId): Promise<void> {
if (this.transport === null) return;
try {
await this.transport.send(buildUnsubscribeEnvelope(jobId, sessionId));
} catch {
// best-effort
}
}
// -------------------------------------------------------------------
private async dispatchRaw(frame: WireFrame): Promise<void> {
await dispatchEnvelope(
{
logger: this.logger,
state: this.state,
handshake: this.handshake,
invocationsByOriginId: this.invocationsByOriginId,
invocationsByJobId: this.invocationsByJobId,
pendingAccepts: this.pendingAccepts,
pendingLists: this.pendingLists,
pendingSubscribes: this.pendingSubscribes,
handlers: this.handlers as Map<
string,
(env: Envelope) => Promise<void>
>,
transport: this.transport,
observeEventSeq: (env) => {
if (
env.event_seq !== undefined &&
env.event_seq > this.lastEventSeq
) {
this.lastEventSeq = env.event_seq;
this.scheduleAutoAck();
}
},
},
frame,
);
}
private scheduleAutoAck(): void {
if (this.autoAckOpts === null) return;
if (!this.hasFeature("ack")) return;
const { intervalMs, minSeqDelta } = this.autoAckOpts;
const delta = this.lastEventSeq - this.lastAckedSeq;
if (delta >= minSeqDelta) {
// Fire immediately (still async-safe).
void this.flushAutoAck().catch(() => undefined);
return;
}
if (this.autoAckTimer !== null) return;
this.autoAckTimer = setTimeout(() => {
this.autoAckTimer = null;
void this.flushAutoAck().catch(() => undefined);
}, intervalMs);
this.autoAckTimer.unref();
}
private async flushAutoAck(): Promise<void> {
if (this.lastEventSeq <= this.lastAckedSeq) return;
if (!this.hasFeature("ack")) return;
try {
await this.ack(this.lastEventSeq);
} catch {
// best-effort
}
}
}
/**
* Lightweight typed assertion used in tests and bridge code: narrow an
* envelope to a specific type. Returns null if the type does not match.
*/
export function asEnvelopeOfType<T extends Envelope["type"]>(
env: Envelope,
type: T,
): Extract<Envelope, { type: T }> | null {
return env.type === type ? (env as Extract<Envelope, { type: T }>) : null;
}
// Silence unused — re-exported for future timers.
void randomBytes;