@myagentroam/agent 0.15.8 → 0.15.10

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.
@@ -114,7 +114,7 @@ export interface ExtensionSessionBackend {
114
114
  export interface SessionInputEntry {
115
115
  /** SDK-owned admission barrier, never accepted from Extension input. */
116
116
  nativeSteer?: {
117
- readonly ready: Promise<void>;
117
+ pending?: true;
118
118
  recordId?: string;
119
119
  restored?: true;
120
120
  };
@@ -151,6 +151,7 @@ export declare class SessionInputCoordinator {
151
151
  recordCompactCommit(outcome: ExtensionSessionActionResult): void;
152
152
  performCompact(input: ExtensionCompactInput, call: ExtensionOperationContext, action: (commit: (outcome: ExtensionSessionActionResult) => void) => Promise<ExtensionSessionActionResult>): Promise<ExtensionSessionActionResult>;
153
153
  accept(request: ExtensionSessionRequest, source: ExtensionOperationContext, action: ExtensionSessionAction, status: 'accepted' | 'queued'): SessionInputEntry;
154
+ cancelSteerReservation(entry: SessionInputEntry): void;
154
155
  handled(input: ExtensionSubmitInput, call: ExtensionOperationContext, value?: unknown): ExtensionSessionReceipt;
155
156
  drain(leaf: (entry: SessionInputEntry, request: ExtensionSessionRequest, call: ExtensionOperationContext) => Promise<ExtensionSessionActionResult>, execution?: {
156
157
  readonly executionId: string;
@@ -57,7 +57,7 @@ export class SessionInputCoordinator {
57
57
  !['cancelled', 'failed'].includes(previous.receipt.status))
58
58
  return;
59
59
  const entry = this.#record({ kind: 'submit', input: { text } }, source, (request, call, next) => next(request, call), 'accepted');
60
- entry.nativeSteer = { recordId, ready: Promise.resolve(), restored: true };
60
+ entry.nativeSteer = { recordId, restored: true };
61
61
  if (index >= 0)
62
62
  this.#pending[index] = entry;
63
63
  else
@@ -65,7 +65,9 @@ export class SessionInputCoordinator {
65
65
  }
66
66
  get hasBoundaryPending() {
67
67
  const head = this.#pending.find((entry) => entry.receipt.status !== 'cancelled' && !entry.source.signal.aborted);
68
- return !!head && (head.request.kind !== 'submit' || head.request.input.delivery !== 'idle');
68
+ return (!!head &&
69
+ !head.nativeSteer?.pending &&
70
+ (head.request.kind !== 'submit' || head.request.input.delivery !== 'idle'));
69
71
  }
70
72
  get executing() {
71
73
  return this.#current.getStore()?.receipt.status === 'in-progress';
@@ -74,6 +76,8 @@ export class SessionInputCoordinator {
74
76
  for (const entry of this.#pending) {
75
77
  if (entry.receipt.status === 'cancelled' || entry.source.signal.aborted)
76
78
  continue;
79
+ if (entry.nativeSteer?.pending)
80
+ return false;
77
81
  if (entry.request.kind === 'submit' && entry.request.input.delivery === 'idle')
78
82
  return false;
79
83
  if (entry.request.kind === 'compact')
@@ -145,6 +149,11 @@ export class SessionInputCoordinator {
145
149
  this.#pending.push(entry);
146
150
  return entry;
147
151
  }
152
+ cancelSteerReservation(entry) {
153
+ // The admission owner calls this only when the writer cannot still commit.
154
+ if (entry.nativeSteer && !entry.nativeSteer.recordId && this.#active.has(entry))
155
+ this.#finish(entry, 'cancelled');
156
+ }
148
157
  handled(input, call, value) {
149
158
  const entry = this.#record({ kind: 'submit', input }, call, (request, effective, next) => next(request, effective), 'accepted');
150
159
  this.#finish(entry, 'handled', { changed: false });
@@ -236,6 +245,11 @@ export class SessionInputCoordinator {
236
245
  !head.source.signal.aborted &&
237
246
  head.receipt.status !== 'cancelled')
238
247
  break;
248
+ // Reservations preserve FIFO order but never block a model boundary on a Hook or writer.
249
+ if (head.nativeSteer?.pending &&
250
+ !head.source.signal.aborted &&
251
+ head.receipt.status !== 'cancelled')
252
+ break;
239
253
  const entry = this.#pending.shift();
240
254
  if (entry.receipt.status === 'cancelled' || entry.source.signal.aborted) {
241
255
  this.#finish(entry, 'cancelled');
@@ -278,13 +292,10 @@ export class SessionInputCoordinator {
278
292
  let effects = 0;
279
293
  let committed;
280
294
  try {
281
- if (entry.nativeSteer) {
282
- await entry.nativeSteer.ready;
295
+ if (entry.nativeSteer && !entry.nativeSteer.recordId) {
283
296
  call.signal.throwIfAborted();
284
- if (!entry.nativeSteer.recordId) {
285
- this.#finish(entry, 'handled', { changed: false });
286
- return false;
287
- }
297
+ this.#finish(entry, 'handled', { changed: false });
298
+ return false;
288
299
  }
289
300
  const outcome = await this.#current.run(entry, () => entry.action(entry.request, call, async (request, effective) => {
290
301
  validateCoreSessionRequest(request);
@@ -7,9 +7,8 @@ import type { SteerInput, SteerResult } from './types.js';
7
7
  export declare function createNativeSteer(input: {
8
8
  readonly executionId: string;
9
9
  readonly inputs: SessionInputCoordinator;
10
- readonly signal: AbortSignal;
11
- isOpen(): boolean;
12
- operation<T>(action: (call: ExtensionOperationContext) => T): T;
10
+ readonly admissionSignal: AbortSignal;
11
+ operation<T>(action: (call: ExtensionOperationContext) => T, signal?: AbortSignal): T;
13
12
  admit(content: ExtensionPromptInput, call: ExtensionOperationContext, write: (content: ExtensionPromptInput, call: ExtensionOperationContext) => Promise<ExtensionPromptResult>): Promise<unknown>;
14
13
  receive(content: ExtensionPromptInput, clientMessageId: string, call: ExtensionOperationContext, source?: import('../extensions/session-input.js').ExtensionInputSource): Promise<SessionRecord>;
15
14
  notify(entry: Extract<SteerResult, {
@@ -11,7 +11,7 @@ export function createNativeSteer(input) {
11
11
  const previous = retries.get(request.clientMessageId);
12
12
  if (previous)
13
13
  return previous;
14
- if (!input.isOpen() || input.signal.aborted || input.inputs.hasIdleBarrier)
14
+ if (input.admissionSignal.aborted || input.inputs.hasIdleBarrier)
15
15
  return Promise.resolve({ status: 'notAccepted' });
16
16
  if (typeof request.clientMessageId !== 'string' ||
17
17
  !request.clientMessageId.trim() ||
@@ -23,25 +23,54 @@ export function createNativeSteer(input) {
23
23
  ...(request.images === undefined ? {} : { images: request.images })
24
24
  });
25
25
  const result = Promise.withResolvers();
26
- const ready = Promise.withResolvers();
27
26
  const entry = input.operation((call) => input.inputs.accept({ kind: 'submit', input: content }, call, (request, effective, next) => next(request, effective), 'accepted'));
28
- entry.nativeSteer = { ready: ready.promise };
29
- const aborted = () => ready.resolve();
30
- input.signal.addEventListener('abort', aborted, { once: true });
27
+ const reservation = { pending: true };
28
+ entry.nativeSteer = reservation;
29
+ const releaseAdmission = () => {
30
+ delete reservation.pending;
31
+ };
32
+ const admissionController = new AbortController();
33
+ let writing = false;
31
34
  let committed = false;
32
- const admission = admissionTail
33
- .catch(() => undefined)
34
- .then(() => input.operation((call) => input.admit(content, call, async (actual, effective) => {
35
+ const releaseReservation = () => {
36
+ input.inputs.cancelSteerReservation(entry);
37
+ releaseAdmission();
38
+ };
39
+ const aborted = () => {
40
+ admissionController.abort(input.admissionSignal.reason);
41
+ // A writer that has started owns the outcome until it reports whether it committed.
42
+ if (!writing) {
43
+ releaseReservation();
44
+ result.resolve({ status: 'notAccepted' });
45
+ }
46
+ };
47
+ input.admissionSignal.addEventListener('abort', aborted, { once: true });
48
+ const write = async (actual, effective) => {
35
49
  if (committed)
36
50
  return {};
37
51
  effective.signal.throwIfAborted();
38
- if (!input.isOpen()) {
39
- result.resolve({ status: 'notAccepted' });
40
- return {};
52
+ writing = true;
53
+ let record;
54
+ try {
55
+ record = await input.receive(actual, request.clientMessageId, effective, source);
56
+ }
57
+ catch (error) {
58
+ if (admissionController.signal.aborted) {
59
+ releaseReservation();
60
+ // I/O errors can leave a durable record. Only the writer's cancellation proves non-admission.
61
+ if (error === admissionController.signal.reason)
62
+ result.resolve({ status: 'notAccepted' });
63
+ else
64
+ result.reject(error);
65
+ }
66
+ throw error;
67
+ }
68
+ finally {
69
+ writing = false;
41
70
  }
42
- const record = await input.receive(actual, request.clientMessageId, effective, source);
43
71
  committed = true;
44
- entry.nativeSteer.recordId = record.recordId;
72
+ input.admissionSignal.removeEventListener('abort', aborted);
73
+ reservation.recordId = record.recordId;
45
74
  const payload = record.payload;
46
75
  const images = imageMetadata(payload.images);
47
76
  const historyEntry = {
@@ -58,24 +87,28 @@ export function createNativeSteer(input) {
58
87
  };
59
88
  // Neither Hook post-processing nor a failed observer can revoke the writer's receipt.
60
89
  void input.notify(historyEntry).catch(() => input.diagnose());
61
- ready.resolve();
90
+ releaseAdmission();
62
91
  result.resolve({ status: 'accepted', entry: historyEntry });
63
92
  input.wake();
64
93
  return {};
65
- })));
94
+ };
95
+ const admission = admissionTail
96
+ .catch(() => undefined)
97
+ .then(() => input.operation((call) => input.admit(content, call, write), admissionController.signal));
66
98
  void admission
67
99
  .then(() => {
68
- if (!committed)
100
+ releaseAdmission();
101
+ if (!committed && !admissionController.signal.aborted)
69
102
  result.resolve({ status: 'handled' });
70
103
  }, (error) => {
71
- if (!committed)
72
- result.reject(error);
73
- else
104
+ releaseAdmission();
105
+ if (committed)
74
106
  input.diagnose();
107
+ else if (!admissionController.signal.aborted)
108
+ result.reject(error);
75
109
  })
76
110
  .finally(() => {
77
- ready.resolve();
78
- input.signal.removeEventListener('abort', aborted);
111
+ input.admissionSignal.removeEventListener('abort', aborted);
79
112
  });
80
113
  admissionTail = result.promise;
81
114
  retries.set(request.clientMessageId, result.promise);
@@ -83,7 +83,6 @@ export function createSessionExecutionFactory(ports) {
83
83
  const manualCompactionId = mode === 'compact' ? randomUUID() : undefined;
84
84
  sessionRuntime.registerExecution(executionOwner);
85
85
  const inputs = sessionInputs();
86
- let acceptsSteer = false;
87
86
  let exhaustedModelAttempt;
88
87
  const eventPorts = {
89
88
  hasRetainedLease: () => store.hasRetainedSessionLease(sessionId, sessionWriterOwner),
@@ -300,14 +299,14 @@ export function createSessionExecutionFactory(ports) {
300
299
  const selectedModelForTools = executionModels.get(input.modelId ?? executionDefaultModelId);
301
300
  const coreExtensions = await extensionManager.prepare(sessionId, controller.signal, sourceBindings, sessionSource.type === 'user' && !internal.automaticInput);
302
301
  executionOwner.bindViews({ toolSources: sources });
303
- const operation = (run) => extensionManager.operations.run({
302
+ const operation = (run, signal) => extensionManager.operations.run({
304
303
  sessionId,
305
304
  sessionEpoch: coreExtensions.epoch,
306
305
  executionId,
307
306
  turnId,
308
307
  actor: 'sdk',
309
308
  entry: 'native-api',
310
- signal: controller.signal
309
+ signal: signal ? AbortSignal.any([controller.signal, signal]) : controller.signal
311
310
  }, run);
312
311
  if (mode !== 'compact') {
313
312
  // Missing entries prepend in reverse order; warm entries retain their FIFO position.
@@ -509,12 +508,10 @@ export function createSessionExecutionFactory(ports) {
509
508
  });
510
509
  if (!entered)
511
510
  return { inputHandled: true };
512
- acceptsSteer = true;
513
511
  executionOwner.acceptSteer(createNativeSteer({
514
512
  executionId,
515
513
  inputs,
516
- signal: controller.signal,
517
- isOpen: () => acceptsSteer,
514
+ admissionSignal: executionOwner.steerAdmissionSignal,
518
515
  operation,
519
516
  admit: (content, call, write) => invokeCorePromptSubmission(coreExtensions.hooks, content, call, write),
520
517
  receive: receiveSteer,
@@ -841,9 +838,7 @@ export function createSessionExecutionFactory(ports) {
841
838
  mailbox: {
842
839
  hasPending: () => inputs.hasBoundaryPending && !inputs.executing,
843
840
  append: appendMailboxMessages,
844
- close: () => {
845
- acceptsSteer = false;
846
- }
841
+ close: () => executionOwner.closeSteerAdmission()
847
842
  }
848
843
  });
849
844
  compactRequests.seal();
@@ -868,7 +863,6 @@ export function createSessionExecutionFactory(ports) {
868
863
  };
869
864
  },
870
865
  beforeFailure: async (error) => {
871
- acceptsSteer = false;
872
866
  executionOwner.stopSteer();
873
867
  await sessionRuntime.joinRecordProjection();
874
868
  await compactCoordinator.close(error).catch(() => undefined);
@@ -86,9 +86,11 @@ export declare class SessionExecution {
86
86
  }) | (import("./types.js").AgentEventBase & {
87
87
  type: "execution.cancelled";
88
88
  }) | undefined;
89
+ get steerAdmissionSignal(): AbortSignal;
89
90
  private readonly steer;
90
91
  bindViews(views: Partial<Pick<SessionExecutionContext, 'requestMessages' | 'appendedMessages' | 'toolSources'>>): void;
91
92
  acceptSteer(steer: NonNullable<SessionExecutionContext['steer']>): void;
93
+ closeSteerAdmission(): void;
92
94
  stopSteer(): void;
93
95
  retireCompactRequests(reason: unknown): void;
94
96
  revokeViews(reason: unknown): void;
@@ -24,6 +24,10 @@ export class SessionExecution {
24
24
  #bindings;
25
25
  // The handle exists before its first input is committed. This only gates entry to the mailbox.
26
26
  #steerReady = Promise.withResolvers();
27
+ #steerAdmission = new AbortController();
28
+ get steerAdmissionSignal() {
29
+ return this.#steerAdmission.signal;
30
+ }
27
31
  steer = async (input, source) => {
28
32
  if (input.expectedExecutionId !== this.executionId)
29
33
  return { status: 'notAccepted' };
@@ -35,11 +39,21 @@ export class SessionExecution {
35
39
  Object.assign(this.#bindings, views);
36
40
  }
37
41
  acceptSteer(steer) {
42
+ if (this.steerAdmissionSignal.aborted)
43
+ return;
38
44
  this.#bindings = { ...this.#bindings, steer };
39
45
  this.#steerReady.resolve();
40
46
  }
47
+ closeSteerAdmission() {
48
+ if (this.steerAdmissionSignal.aborted)
49
+ return;
50
+ // Keep the binding for retries of already accepted IDs until the Execution tears down.
51
+ this.#steerReady.resolve();
52
+ this.#steerAdmission.abort(new AgentError('MAR_AGENT_CANCELLED', 'Steer admission closed.'));
53
+ }
41
54
  stopSteer() {
42
55
  delete this.#bindings.steer;
56
+ this.closeSteerAdmission();
43
57
  }
44
58
  retireCompactRequests(reason) {
45
59
  this.compactRequests.close(reason);
@@ -50,7 +64,6 @@ export class SessionExecution {
50
64
  this.retireCompactRequests(reason);
51
65
  delete this.#bindings.appendedMessages;
52
66
  this.stopSteer();
53
- this.#steerReady.resolve();
54
67
  }
55
68
  detachToolSources() {
56
69
  delete this.#bindings.toolSources;
@@ -225,6 +238,8 @@ export class SessionExecution {
225
238
  if (full.type === 'execution.completed') {
226
239
  this.controller.signal.throwIfAborted();
227
240
  this.completionAdmitted = true;
241
+ // Handled first input never binds steer; release startup waiters before Host publication.
242
+ this.closeSteerAdmission();
228
243
  }
229
244
  if (full.type === 'execution.completed' ||
230
245
  full.type === 'execution.failed' ||
@@ -388,10 +403,12 @@ export class SessionExecution {
388
403
  }
389
404
  catch (error) {
390
405
  failed = true;
406
+ // Cleanup can release steer callers that interrupt this execution on fallback.
407
+ const cancelled = this.controller.signal.aborted;
391
408
  await input.beforeFailure(error);
392
409
  input.endParent();
393
410
  const finalAnswer = this.kind === 'compact' ? '' : (this.finalAnswer ?? '');
394
- if (this.controller.signal.aborted) {
411
+ if (cancelled) {
395
412
  await input.emit({ type: 'execution.cancelled' }).catch(() => undefined);
396
413
  return { executionId: this.executionId, status: 'cancelled', finalAnswer };
397
414
  }
@@ -436,7 +453,7 @@ export class SessionExecution {
436
453
  cancel(reason) {
437
454
  if (this.completionAdmitted)
438
455
  return;
456
+ this.closeSteerAdmission();
439
457
  this.controller.abort(reason);
440
- this.#steerReady.resolve();
441
458
  }
442
459
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myagentroam/agent",
3
- "version": "0.15.8",
3
+ "version": "0.15.10",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/myagentroam/mar.git",