@myagentroam/node 0.11.43 → 0.11.44

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.
@@ -75,30 +75,53 @@ export class NodeLifecycle {
75
75
  async stopResources(handoff) {
76
76
  let snapshot;
77
77
  let failure;
78
+ let stage = 'waitForRecovery';
79
+ const failed = (error) => {
80
+ failure ??= { error };
81
+ try {
82
+ this.shutdown.diagnose(stage, error);
83
+ }
84
+ catch {
85
+ // Diagnostics must not replace the shutdown failure or prevent cleanup.
86
+ }
87
+ };
78
88
  try {
79
89
  await this.shutdown.waitForRecovery();
90
+ stage = 'drainInput';
80
91
  await this.shutdown.drainInput();
81
92
  }
82
93
  catch (error) {
83
- failure = error;
94
+ failed(error);
84
95
  }
85
96
  try {
97
+ stage = 'closeResume';
86
98
  await this.shutdown.closeResume();
87
- if (failure === undefined && handoff)
99
+ if (failure === undefined && handoff) {
100
+ stage = 'capture';
88
101
  snapshot = this.shutdown.capture();
102
+ }
89
103
  }
90
104
  catch (error) {
91
- failure ??= error;
105
+ failed(error);
92
106
  }
93
107
  try {
108
+ stage = 'dispose';
94
109
  await this.shutdown.dispose();
95
110
  }
96
111
  catch (error) {
97
- failure ??= error;
112
+ failed(error);
98
113
  }
99
114
  if (failure !== undefined)
100
- throw failure;
101
- if (snapshot !== undefined)
102
- await this.shutdown.publish(snapshot);
115
+ throw failure.error;
116
+ if (snapshot !== undefined) {
117
+ try {
118
+ await this.shutdown.publish(snapshot);
119
+ }
120
+ catch (error) {
121
+ stage = 'publish';
122
+ failed(error);
123
+ throw error;
124
+ }
125
+ }
103
126
  }
104
127
  }
package/dist/connector.js CHANGED
@@ -197,7 +197,11 @@ export class NodeConnector {
197
197
  closeResume: () => this.externalSessionResumeService.close(),
198
198
  capture: () => (this.config?.nodeId ? this.restartHandoff.capture() : undefined),
199
199
  dispose: () => this.disposeRuntime(),
200
- publish: (snapshot) => this.restartHandoff.publish(snapshot)
200
+ publish: (snapshot) => this.restartHandoff.publish(snapshot),
201
+ diagnose: (stage, error) => nodeLog('node.shutdown.failed', {
202
+ stage,
203
+ errorCode: safeErrorCode(error, 'NODE_SHUTDOWN_FAILED')
204
+ })
201
205
  });
202
206
  get stopped() {
203
207
  return this.lifecycle.stopped;
package/dist/main.js CHANGED
@@ -45,10 +45,17 @@ if (command === 'run') {
45
45
  clearTimeout(deadline);
46
46
  releaseOwner();
47
47
  process.exit(exitCode);
48
- }, () => {
48
+ }, (error) => {
49
49
  clearTimeout(deadline);
50
- nodeLog('node.restart.diagnostic', { code: 'RESTART_HANDOFF_SHUTDOWN_FAILED' });
51
- process.exit(1);
50
+ try {
51
+ nodeLog('node.restart.diagnostic', {
52
+ code: 'RESTART_HANDOFF_SHUTDOWN_FAILED',
53
+ errorCode: safeErrorCode(error, 'NODE_SHUTDOWN_FAILED')
54
+ });
55
+ }
56
+ finally {
57
+ process.exit(1);
58
+ }
52
59
  });
53
60
  };
54
61
  const supervisorHandoff = new SupervisorUpgradeHandoff();
@@ -52,7 +52,7 @@ const tools = [
52
52
  },
53
53
  {
54
54
  action: 'send',
55
- description: 'Send input, decide a child approval or update a Goal; goal:null leaves it unchanged, goal:{clear:true} clears it. Prefer REPLACE_CURRENT for timely communication or course correction. QUEUE is consumed only after current work and earlier queued inputs finish; an active Goal must exit first. Sending to a non-child does not subscribe to its updates.',
55
+ description: 'Send input, decide an approval or update a Goal; goal:null leaves it unchanged, goal:{clear:true} clears it. Prefer REPLACE_CURRENT for timely communication or course correction. QUEUE is consumed only after current work and earlier queued inputs finish; an active Goal must exit first. Sending to a non-child does not subscribe to its updates.',
56
56
  properties: {
57
57
  agentId: text,
58
58
  content: { type: 'string' },
@@ -240,19 +240,24 @@ export class MarAgentSessionRuntimeRegistry {
240
240
  throw failure.reason;
241
241
  }
242
242
  async #destroyRuntime(runtime, aliases, reason, runId, changedCategories = []) {
243
- nodeLog('runner.mar-agent.runtime.releasing', {
244
- runtimeId: runtime.runtimeId,
245
- sessionId: runtime.session.id,
246
- reason,
247
- ...(runId === undefined ? {} : { runId }),
248
- ...(changedCategories.length === 0 ? {} : { changedCategories })
249
- });
243
+ try {
244
+ nodeLog('runner.mar-agent.runtime.releasing', {
245
+ runtimeId: runtime.runtimeId,
246
+ sessionId: runtime.session.id,
247
+ reason,
248
+ ...(runId === undefined ? {} : { runId }),
249
+ ...(changedCategories.length === 0 ? {} : { changedCategories })
250
+ });
251
+ }
252
+ catch {
253
+ // Resource release must not depend on a writable diagnostic sink.
254
+ }
250
255
  const results = await Promise.allSettled([
251
256
  this.#releaseComputerAliases(new Set([...aliases, runtime.session.id]))
252
257
  ]);
253
258
  results.push(...(await Promise.allSettled([releaseSessionRuntime(runtime)])));
254
259
  const failures = results.filter((result) => result.status === 'rejected');
255
- if (failures.length > 1)
260
+ if (failures.length > 0)
256
261
  this.#logCleanupFailure(runtime.session.id);
257
262
  if (failures[0]?.status === 'rejected')
258
263
  throw failures[0].reason;
@@ -263,9 +268,14 @@ export class MarAgentSessionRuntimeRegistry {
263
268
  this.#entries.delete(alias);
264
269
  }
265
270
  #logCleanupFailure(sessionId) {
266
- nodeLog('runner.mar-agent.runtime.cleanup_failed', {
267
- sessionId,
268
- code: 'MAR_AGENT_RUNTIME_CLEANUP_FAILED'
269
- });
271
+ try {
272
+ nodeLog('runner.mar-agent.runtime.cleanup_failed', {
273
+ sessionId,
274
+ code: 'MAR_AGENT_RUNTIME_CLEANUP_FAILED'
275
+ });
276
+ }
277
+ catch {
278
+ // Keep the original cleanup error when the diagnostic sink is unavailable.
279
+ }
270
280
  }
271
281
  }
@@ -185,25 +185,30 @@ export async function reconfigureSessionRuntime(existing, input, modelFactory) {
185
185
  }
186
186
  export async function releaseSessionRuntime(runtime) {
187
187
  const failures = [];
188
- for (const cleanup of [
189
- () => runtime.agent.dispose(),
190
- () => runtime.releaseNetworkContext?.(),
191
- () => runtime.mcpLease.release(),
192
- () => runtime.releaseEnvironment?.()
188
+ for (const [resource, cleanup] of [
189
+ ['agent', () => runtime.agent.dispose()],
190
+ ['network', () => runtime.releaseNetworkContext?.()],
191
+ ['mcp', () => runtime.mcpLease.release()],
192
+ ['environment', () => runtime.releaseEnvironment?.()]
193
193
  ]) {
194
194
  try {
195
195
  await cleanup();
196
196
  }
197
197
  catch (error) {
198
198
  failures.push(error);
199
+ try {
200
+ nodeLog('runner.mar-agent.runtime.cleanup_failed', {
201
+ sessionId: runtime.session.id,
202
+ code: 'MAR_AGENT_RUNTIME_CLEANUP_FAILED',
203
+ stage: 'resource_release',
204
+ resource
205
+ });
206
+ }
207
+ catch {
208
+ // Keep releasing resources even when the diagnostic sink is unavailable.
209
+ }
199
210
  }
200
211
  }
201
- if (failures.length > 1)
202
- nodeLog('runner.mar-agent.runtime.cleanup_failed', {
203
- sessionId: runtime.session.id,
204
- code: 'MAR_AGENT_RUNTIME_CLEANUP_FAILED',
205
- stage: 'resource_release'
206
- });
207
212
  if (failures.length > 0)
208
213
  throw failures[0];
209
214
  }
@@ -3,10 +3,8 @@ import { resolvedPersonalInstructions } from '../runner/personal-instructions.js
3
3
  import { AgentNetworkService } from './agent-network-service.js';
4
4
  import { isPlainRecord } from '../util/records.js';
5
5
  import { safeErrorCode } from '../util/safe-error.js';
6
- import { withSessionMessageOrigin } from './composer-message-input.js';
7
6
  import { SESSION_ADMISSION_SIGNAL } from '../session/operation-admission.js';
8
7
  import { assertSessionControlOwner } from './run-ownership-policy.js';
9
- const approvedCalls = new WeakMap();
10
8
  const terminalCalls = new WeakMap();
11
9
  /** Bind a terminal actor only after the Node request boundary has authenticated its caller. */
12
10
  export function actorForAuthenticatedTerminal(actor, workspaceId) {
@@ -14,14 +12,6 @@ export function actorForAuthenticatedTerminal(actor, workspaceId) {
14
12
  terminalCalls.set(authenticated, { workspaceId });
15
13
  return authenticated;
16
14
  }
17
- /** Bind a Host-approved invocation to its exact target and domain operations. */
18
- export function actorForApprovedAgentCall(actor, context, agentIds, operations) {
19
- if (context.permission?.decision !== 'approved')
20
- return actor;
21
- const approved = Object.freeze({ ...actor });
22
- approvedCalls.set(approved, { agentIds: [...agentIds], operations: [...operations] });
23
- return approved;
24
- }
25
15
  import { sessionPage } from '../session/pagination.js';
26
16
  import { marctlSessionSummary, marctlSessionDetail } from './session-recent-activity.js';
27
17
  /** Bind the collaboration use case to the existing Session, Run and Goal owners. */
@@ -50,7 +40,7 @@ export function createNodeAgentNetwork(options) {
50
40
  const parent = terminal ? undefined : await actorSession(actor);
51
41
  const workspaceId = terminal?.workspaceId ?? parent?.workspaceId;
52
42
  const session = options.runtime.getAgentSession(id) ?? (await options.projection.resolve(id, true));
53
- if (!session || session.workspaceId !== workspaceId || session.id === parent?.id)
43
+ if (!session || session.workspaceId !== workspaceId)
54
44
  throw new Error('SESSION_NOT_FOUND');
55
45
  return session;
56
46
  }
@@ -93,11 +83,8 @@ export function createNodeAgentNetwork(options) {
93
83
  return result;
94
84
  }
95
85
  async function validateApproval(actor, session, input) {
96
- if (!terminalCalls.has(actor)) {
97
- const parent = await actorSession(actor);
98
- if (session.parentSessionId !== parent.id)
99
- throw new Error('AGENT_TARGET_NOT_AUTHORIZED');
100
- }
86
+ if (!terminalCalls.has(actor))
87
+ await actorSession(actor);
101
88
  assertSessionControlOwner(options.runtime, session.id, actor.requestUserId);
102
89
  const approval = options.runtime.getApproval(input.approvalId);
103
90
  if (!approval)
@@ -118,19 +105,10 @@ export function createNodeAgentNetwork(options) {
118
105
  return new AgentNetworkService({
119
106
  observations: options.observations,
120
107
  target,
121
- authorize: async (actor, session, operation) => {
108
+ authorize: async (actor, session) => {
109
+ if (!terminalCalls.has(actor))
110
+ await actorSession(actor);
122
111
  assertSessionControlOwner(options.runtime, session.id, actor.requestUserId);
123
- if (terminalCalls.has(actor))
124
- return;
125
- const source = await actorSession(actor);
126
- if (session.parentSessionId === source.id)
127
- return;
128
- if (source.parentSessionId === session.id && operation === 'send')
129
- return;
130
- const approved = approvedCalls.get(actor);
131
- if (approved?.agentIds.includes(session.id) && approved.operations.includes(operation))
132
- return;
133
- throw new Error('AGENT_TARGET_NOT_AUTHORIZED');
134
112
  },
135
113
  children: async (actor) => {
136
114
  if (terminalCalls.has(actor))
@@ -144,27 +122,6 @@ export function createNodeAgentNetwork(options) {
144
122
  observe: (actor) => {
145
123
  options.ensureObservations(actor);
146
124
  },
147
- isUpward: async (actor, session) => !terminalCalls.has(actor) && (await actorSession(actor)).parentSessionId === session.id,
148
- reportUpward: async (actor, session, input, signal) => {
149
- const child = await actorSession(actor);
150
- const runId = actor.runId;
151
- const executionId = actor.executionId;
152
- if (!runId ||
153
- !executionId ||
154
- options.runtime.currentExecutionId(runId) !== executionId ||
155
- child.parentSessionId !== session.id)
156
- throw new Error('AGENT_ACTOR_NOT_ACTIVE');
157
- signal?.throwIfAborted();
158
- const parent = options.observations.get(session.id);
159
- if (!parent?.report({
160
- childId: child.id,
161
- runId,
162
- executionId,
163
- value: withSessionMessageOrigin(input.content, origin(child), session.parentSessionId)
164
- }))
165
- throw new Error('AGENT_PARENT_NOT_WARM');
166
- return { delivery: 'reported', clientMessageId: input.clientMessageId };
167
- },
168
125
  freezeExecution: (session) => {
169
126
  const snapshot = options.runtime.latestSessionExecution(session.id);
170
127
  const run = options.runtime.activeSessionRun(session.id);
@@ -1,4 +1,4 @@
1
- import { actorForApprovedAgentCall, actorForAuthenticatedTerminal } from './agent-network-bindings.js';
1
+ import { actorForAuthenticatedTerminal } from './agent-network-bindings.js';
2
2
  import { isPlainRecord } from '../util/records.js';
3
3
  import { randomUUID } from 'node:crypto';
4
4
  import { modelUsageGrantSchema } from '@myagentroam/protocol';
@@ -52,21 +52,6 @@ export function executionAgentActor(input) {
52
52
  }
53
53
  /** Shared command dispatch; callers must bind actor authority at their trusted boundary. */
54
54
  export async function invokeAgentNetwork(service, actor, action, payload, context, requestSignal) {
55
- const targets = typeof payload.agentId === 'string'
56
- ? [payload.agentId]
57
- : Array.isArray(payload.agentIds)
58
- ? payload.agentIds.filter((id) => typeof id === 'string')
59
- : [];
60
- const operations = action === 'send'
61
- ? ['send', 'goal']
62
- : action === 'stop'
63
- ? ['stop']
64
- : action === 'wait'
65
- ? ['wait']
66
- : [];
67
- const authorized = context
68
- ? actorForApprovedAgentCall(actor, context, targets, operations)
69
- : actor;
70
55
  const signal = context?.signal ?? requestSignal;
71
56
  switch (action) {
72
57
  case 'start':
@@ -85,11 +70,11 @@ export async function invokeAgentNetwork(service, actor, action, payload, contex
85
70
  ...(typeof payload.title === 'string' ? { title: payload.title.trim() } : {})
86
71
  }, signal);
87
72
  case 'send':
88
- return service.send(authorized, payload, signal);
73
+ return service.send(actor, payload, signal);
89
74
  case 'stop':
90
- return service.stop(authorized, payload);
75
+ return service.stop(actor, payload);
91
76
  case 'wait':
92
- return service.wait(authorized, {
77
+ return service.wait(actor, {
93
78
  ...payload,
94
79
  ...(context
95
80
  ? { signal: context.signal, interrupt: context.waitInterrupt }
@@ -91,22 +91,6 @@ export class AgentNetworkService {
91
91
  if (input.approval !== undefined)
92
92
  await this.options.validateApproval(actor, session, input.approval);
93
93
  signal?.throwIfAborted();
94
- if (hasMessage && (await this.options.isUpward(actor, session))) {
95
- signal?.throwIfAborted();
96
- if (goal.kind !== 'unchanged' ||
97
- input.approval !== undefined ||
98
- input.deliveryIntent === 'REPLACE_CURRENT' ||
99
- input.deliveryIntent === 'QUEUE' ||
100
- input.attachments?.length ||
101
- input.attachmentIds?.length)
102
- throw new Error('AGENT_UPWARD_REPORT_INVALID');
103
- const result = await this.options.reportUpward(actor, session, {
104
- clientMessageId: input.clientMessageId ?? randomUUID(),
105
- content
106
- }, signal);
107
- return { agentId: session.id, message: { status: 'accepted', result } };
108
- }
109
- signal?.throwIfAborted();
110
94
  if (session.parentSessionId === actor.sessionId)
111
95
  this.options.observe(actor, session);
112
96
  const approvalResult = input.approval === undefined
@@ -21,7 +21,7 @@ const executionManagementOperations = new Set([
21
21
  'session.config.update',
22
22
  'session.schedule.agent.save'
23
23
  ]);
24
- /** Prevent execution credentials from reaching legacy writes outside their Session tree. */
24
+ /** Keep execution management within its authenticated Workspace and Run ownership. */
25
25
  export async function authorizeMarctlExecutionOperation(input) {
26
26
  if (!isPlainRecord(input.caller))
27
27
  throw new Error('MARCTL_EXECUTION_CREDENTIAL_INVALID');
@@ -31,19 +31,27 @@ export async function authorizeMarctlExecutionOperation(input) {
31
31
  throw new Error('MARCTL_EXECUTION_CREDENTIAL_INVALID');
32
32
  if (executionReadOperations.has(input.operation))
33
33
  return;
34
- if (!executionManagementOperations.has(input.operation) ||
35
- !isPlainRecord(input.data) ||
36
- typeof input.data.sessionId !== 'string' ||
37
- typeof input.data.workspaceId !== 'string')
34
+ if (!isPlainRecord(input.data) || typeof input.data.workspaceId !== 'string')
38
35
  throw new Error('MARCTL_AGENT_NETWORK_REQUIRED');
36
+ let sessionId;
37
+ if (input.operation === 'run.approval.decide') {
38
+ const run = typeof input.data.runId === 'string' ? input.runtime.getRun(input.data.runId) : undefined;
39
+ if (!run)
40
+ throw new Error('RUN_NOT_FOUND');
41
+ sessionId = run.sessionId;
42
+ }
43
+ else {
44
+ if (!executionManagementOperations.has(input.operation) ||
45
+ typeof input.data.sessionId !== 'string')
46
+ throw new Error('MARCTL_AGENT_NETWORK_REQUIRED');
47
+ sessionId = input.data.sessionId;
48
+ }
39
49
  const source = input.runtime.getAgentSession(input.caller.sessionId);
40
- const target = input.runtime.getAgentSession(input.data.sessionId) ??
41
- (await input.resolveSession(input.data.sessionId));
50
+ const target = input.runtime.getAgentSession(sessionId) ?? (await input.resolveSession(sessionId));
42
51
  if (!source ||
43
52
  !target ||
44
53
  source.workspaceId !== input.data.workspaceId ||
45
- target.workspaceId !== source.workspaceId ||
46
- (target.id !== source.id && target.parentSessionId !== source.id))
54
+ target.workspaceId !== source.workspaceId)
47
55
  throw new Error('AGENT_TARGET_NOT_AUTHORIZED');
48
56
  assertSessionControlOwner(input.runtime, target.id, input.userId);
49
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myagentroam/node",
3
- "version": "0.11.43",
3
+ "version": "0.11.44",
4
4
  "description": "MyAgentRoam Node runtime CLI.",
5
5
  "license": "Apache-2.0 AND GPL-2.0-or-later",
6
6
  "type": "module",
@@ -38,10 +38,10 @@
38
38
  "sharp": "0.35.5",
39
39
  "ws": "^8.21.3",
40
40
  "zod": "4.4.3",
41
- "@myagentroam/agent": "0.11.43",
41
+ "@myagentroam/agent": "0.11.44",
42
42
  "@myagentroam/marctl": "0.11.6",
43
- "@myagentroam/code-navigation": "0.11.1",
44
- "@myagentroam/protocol": "0.11.43"
43
+ "@myagentroam/protocol": "0.11.44",
44
+ "@myagentroam/code-navigation": "0.11.1"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/ws": "^8.18.1"