@moxt-ai/mobius 0.0.35 → 0.0.36

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.
package/dist/runtime.js CHANGED
@@ -22701,6 +22701,12 @@ var sessionIdentityFields = {
22701
22701
  };
22702
22702
  var sessionPrepareCommandSchema = external_exports.strictObject({
22703
22703
  ...sessionIdentityFields,
22704
+ configuration: external_exports.strictObject({
22705
+ booleanValue: external_exports.boolean(),
22706
+ configId: external_exports.string().min(1).max(128),
22707
+ value: external_exports.string().max(500),
22708
+ valueType: external_exports.enum(["boolean", "select"])
22709
+ }).readonly().optional(),
22704
22710
  kind: external_exports.literal(SESSION_PREPARE_KIND),
22705
22711
  requestId: IDENTIFIER,
22706
22712
  transient: external_exports.boolean().optional()
@@ -22718,6 +22724,12 @@ var sessionReadyEventSchema = external_exports.strictObject({
22718
22724
  kind: external_exports.literal(SESSION_READY_KIND),
22719
22725
  requestId: IDENTIFIER,
22720
22726
  restored: external_exports.boolean(),
22727
+ runtimeTimings: external_exports.array(
22728
+ external_exports.strictObject({
22729
+ durationMilliseconds: external_exports.number().finite().nonnegative().max(12e4),
22730
+ name: external_exports.string().regex(/^[a-z][a-z0-9-]{0,63}$/)
22731
+ }).readonly()
22732
+ ).max(16).readonly().optional(),
22721
22733
  sessionId: IDENTIFIER
22722
22734
  }).readonly();
22723
22735
  var sessionConfigureCommandSchema = external_exports.strictObject({
@@ -24372,6 +24384,38 @@ async function readAuthorizedRaster(uri, roots, signal) {
24372
24384
  return new LocalRasterImage(basename(target) || "Image", mimeType, bytes);
24373
24385
  }
24374
24386
 
24387
+ // src/infra/operation-metrics.ts
24388
+ var AccumulatingOperationMetrics = class {
24389
+ #durations = /* @__PURE__ */ new Map();
24390
+ measure = async (name, operation) => {
24391
+ const startedAt = performance.now();
24392
+ try {
24393
+ return await operation();
24394
+ } finally {
24395
+ this.record(name, performance.now() - startedAt);
24396
+ }
24397
+ };
24398
+ measureSync = (name, operation) => {
24399
+ const startedAt = performance.now();
24400
+ try {
24401
+ return operation();
24402
+ } finally {
24403
+ this.record(name, performance.now() - startedAt);
24404
+ }
24405
+ };
24406
+ snapshot = () => Object.freeze(
24407
+ [...this.#durations].map(([name, durationMilliseconds]) => Object.freeze({ durationMilliseconds, name }))
24408
+ );
24409
+ record = (name, durationMilliseconds) => {
24410
+ this.#durations.set(name, (this.#durations.get(name) ?? 0) + Math.max(0, durationMilliseconds));
24411
+ };
24412
+ };
24413
+ var unmeasuredOperationMetrics = Object.freeze({
24414
+ measure: async (_name, operation) => await operation(),
24415
+ measureSync: (_name, operation) => operation(),
24416
+ snapshot: () => Object.freeze([])
24417
+ });
24418
+
24375
24419
  // src/native-sessions/history-images.ts
24376
24420
  var IMAGE_MARKER = /\[@image\]\(([^)\r\n]*)(?:\)|$)/g;
24377
24421
  async function embedLocalHistoryImages(history, roots, signal) {
@@ -26112,6 +26156,7 @@ var AcpProcessSession = class _AcpProcessSession {
26112
26156
  #pendingPermissions = /* @__PURE__ */ new Map();
26113
26157
  #preSessionUpdates = [];
26114
26158
  #processExit;
26159
+ #metrics;
26115
26160
  #sessionConfigProtocol;
26116
26161
  #store;
26117
26162
  #verifiedAgentVersion;
@@ -26145,13 +26190,14 @@ var AcpProcessSession = class _AcpProcessSession {
26145
26190
  #sessionUpdatedAt = "";
26146
26191
  #turn = new IdleAcpTurn();
26147
26192
  #updateTail = Promise.resolve();
26148
- constructor(agent, cwd, additionalDirectories, mobiusSessionId, getObserver, store) {
26193
+ constructor(agent, cwd, additionalDirectories, mobiusSessionId, getObserver, store, metrics) {
26149
26194
  this.#additionalDirectories = [...additionalDirectories];
26150
26195
  this.#agentId = agent.id;
26151
26196
  this.#cwd = cwd;
26152
26197
  this.#environment = agent.environment;
26153
26198
  this.#getObserver = getObserver;
26154
26199
  this.#mobiusSessionId = mobiusSessionId;
26200
+ this.#metrics = metrics;
26155
26201
  this.#sessionConfigProtocol = sessionConfigProtocolForAgent(agent.id);
26156
26202
  this.#store = store;
26157
26203
  this.#verifiedAgentVersion = agent.agentVersion;
@@ -26180,13 +26226,16 @@ var AcpProcessSession = class _AcpProcessSession {
26180
26226
  const stream = ndJsonStream2(createWritableAcpStream(stdin), readableWebStream(stdout));
26181
26227
  this.#connection = createAcpClient(this).connect(stream);
26182
26228
  }
26183
- static async #start(options, getObserver, store, initialize) {
26229
+ static async #start(options, getObserver, store, initialize, metrics) {
26184
26230
  for (let attempt = 1; attempt <= 2; attempt += 1) {
26185
26231
  options.signal.throwIfAborted();
26186
- const [adapterUnchanged, agentUnchanged] = await Promise.all([
26187
- revalidateExecutable(options.agent.adapterIdentity),
26188
- revalidateExecutable(options.agent.agentIdentity)
26189
- ]);
26232
+ const [adapterUnchanged, agentUnchanged] = await metrics.measure(
26233
+ "executable-validation",
26234
+ async () => await Promise.all([
26235
+ revalidateExecutable(options.agent.adapterIdentity),
26236
+ revalidateExecutable(options.agent.agentIdentity)
26237
+ ])
26238
+ );
26190
26239
  options.signal.throwIfAborted();
26191
26240
  if (!adapterUnchanged || !agentUnchanged) {
26192
26241
  throw new AcpExecutionError(
@@ -26195,13 +26244,17 @@ var AcpProcessSession = class _AcpProcessSession {
26195
26244
  "agent_runtime_changed"
26196
26245
  );
26197
26246
  }
26198
- const runtime = new _AcpProcessSession(
26199
- options.agent,
26200
- options.cwd,
26201
- options.additionalDirectories,
26202
- options.sessionId,
26203
- getObserver,
26204
- store
26247
+ const runtime = metrics.measureSync(
26248
+ "process-spawn",
26249
+ () => new _AcpProcessSession(
26250
+ options.agent,
26251
+ options.cwd,
26252
+ options.additionalDirectories,
26253
+ options.sessionId,
26254
+ getObserver,
26255
+ store,
26256
+ metrics
26257
+ )
26205
26258
  );
26206
26259
  try {
26207
26260
  await initialize(runtime);
@@ -26223,17 +26276,19 @@ var AcpProcessSession = class _AcpProcessSession {
26223
26276
  }
26224
26277
  throw new AcpExecutionError("ACP agent initialization failed");
26225
26278
  }
26226
- static start = async (options, getObserver, store) => await _AcpProcessSession.#start(
26279
+ static start = async (options, getObserver, store, metrics = unmeasuredOperationMetrics) => await _AcpProcessSession.#start(
26227
26280
  options,
26228
26281
  getObserver,
26229
26282
  store,
26230
- async (runtime) => await runtime.initialize(options.signal, options.origin)
26283
+ async (runtime) => await runtime.initialize(options.signal, options.origin),
26284
+ metrics
26231
26285
  );
26232
26286
  static startNativeSessionBrowser = async (options, getObserver, store) => await _AcpProcessSession.#start(
26233
26287
  options,
26234
26288
  getObserver,
26235
26289
  store,
26236
- async (runtime) => await runtime.#initializeAgent(options.signal)
26290
+ async (runtime) => await runtime.#initializeAgent(options.signal),
26291
+ unmeasuredOperationMetrics
26237
26292
  );
26238
26293
  static startSessionForkAtTurn = async (options, sourceNativeSessionId, forkPoint, getObserver, store) => await _AcpProcessSession.#start(
26239
26294
  options,
@@ -26243,7 +26298,8 @@ var AcpProcessSession = class _AcpProcessSession {
26243
26298
  options.signal,
26244
26299
  { kind: "fork", nativeSessionId: sourceNativeSessionId },
26245
26300
  forkPoint
26246
- )
26301
+ ),
26302
+ unmeasuredOperationMetrics
26247
26303
  );
26248
26304
  belongsTo = (agentId, cwd, additionalDirectories) => this.#agentId === agentId && this.#cwd === cwd && arraysEqual(this.#additionalDirectories, additionalDirectories);
26249
26305
  usesEnvironment = (environment) => recordsEqual(this.#environment, environment);
@@ -26362,6 +26418,9 @@ var AcpProcessSession = class _AcpProcessSession {
26362
26418
  );
26363
26419
  };
26364
26420
  async #initializeAgent(signal) {
26421
+ await this.#metrics.measure("agent-initialize", async () => await this.#initializeAgentOperation(signal));
26422
+ }
26423
+ async #initializeAgentOperation(signal) {
26365
26424
  const initialized = await this.race(
26366
26425
  this.#connection.agent.request(
26367
26426
  methods6.agent.initialize,
@@ -26532,18 +26591,21 @@ var AcpProcessSession = class _AcpProcessSession {
26532
26591
  }
26533
26592
  this.#store.remove(this.#mobiusSessionId);
26534
26593
  }
26535
- const created = await this.race(
26536
- this.#connection.agent.request(
26537
- methods6.agent.session.new,
26538
- {
26539
- additionalDirectories: [...this.#additionalDirectories],
26540
- cwd: this.#cwd,
26541
- mcpServers: []
26542
- },
26543
- { cancellationSignal: signal }
26544
- ),
26545
- "ACP agent exited while starting a session",
26546
- signal
26594
+ const created = await this.#metrics.measure(
26595
+ "session-new",
26596
+ async () => await this.race(
26597
+ this.#connection.agent.request(
26598
+ methods6.agent.session.new,
26599
+ {
26600
+ additionalDirectories: [...this.#additionalDirectories],
26601
+ cwd: this.#cwd,
26602
+ mcpServers: []
26603
+ },
26604
+ { cancellationSignal: signal }
26605
+ ),
26606
+ "ACP agent exited while starting a session",
26607
+ signal
26608
+ )
26547
26609
  );
26548
26610
  this.selectNativeSession(created.sessionId);
26549
26611
  lifecycleResponse = created;
@@ -27346,10 +27408,10 @@ var AcpSessionManager = class {
27346
27408
  this.#nativeBrowserPreparing.set(options.sessionId, operation);
27347
27409
  return await operation;
27348
27410
  }
27349
- async #startPreparation(options) {
27411
+ async #startPreparation(options, metrics = unmeasuredOperationMetrics) {
27350
27412
  const operation = (async () => {
27351
27413
  try {
27352
- const runtime = await AcpProcessSession.start(options, () => this.#observer, this.#store);
27414
+ const runtime = await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics);
27353
27415
  this.#sessions.set(options.sessionId, runtime);
27354
27416
  return runtime;
27355
27417
  } finally {
@@ -27378,13 +27440,13 @@ var AcpSessionManager = class {
27378
27440
  this.#preparing.set(options.sessionId, operation);
27379
27441
  return await operation;
27380
27442
  }
27381
- async #replacePreparation(existing, options) {
27443
+ async #replacePreparation(existing, options, metrics) {
27382
27444
  const operation = (async () => {
27383
27445
  try {
27384
27446
  this.#sessions.delete(options.sessionId);
27385
27447
  await existing.close();
27386
27448
  this.#store.remove(options.sessionId);
27387
- const runtime = await AcpProcessSession.start(options, () => this.#observer, this.#store);
27449
+ const runtime = await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics);
27388
27450
  this.#sessions.set(options.sessionId, runtime);
27389
27451
  return runtime;
27390
27452
  } finally {
@@ -27446,7 +27508,7 @@ var AcpSessionManager = class {
27446
27508
  this.#preparing.set(options.sessionId, operation);
27447
27509
  return await operation;
27448
27510
  }
27449
- prepareSession = async (options) => {
27511
+ prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
27450
27512
  const stored = this.#store.read(options.sessionId);
27451
27513
  if (stored instanceof DeletedAcpStoredSession) {
27452
27514
  throw new AcpExecutionError(
@@ -27464,7 +27526,7 @@ var AcpSessionManager = class {
27464
27526
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
27465
27527
  return runtime2.prepared();
27466
27528
  }
27467
- return await this.prepareSession(options);
27529
+ return await this.prepareSession(options, metrics);
27468
27530
  }
27469
27531
  const existing = this.#sessions.get(options.sessionId);
27470
27532
  if (existing !== void 0) {
@@ -27478,10 +27540,10 @@ var AcpSessionManager = class {
27478
27540
  "session_identity_mismatch"
27479
27541
  );
27480
27542
  }
27481
- const runtime2 = await this.#replacePreparation(existing, options);
27543
+ const runtime2 = await this.#replacePreparation(existing, options, metrics);
27482
27544
  return runtime2.prepared();
27483
27545
  }
27484
- const runtime = await this.#startPreparation(options);
27546
+ const runtime = await this.#startPreparation(options, metrics);
27485
27547
  return runtime.prepared();
27486
27548
  };
27487
27549
  /**
@@ -32058,6 +32120,7 @@ async function executeSessionPrepare(publisher, socket, command, config2, direct
32058
32120
  signal
32059
32121
  );
32060
32122
  const agent = config2.catalog.find(command.agentId);
32123
+ const metrics = new AccumulatingOperationMetrics();
32061
32124
  const preparation = {
32062
32125
  additionalDirectories: resolved.additionalDirectories,
32063
32126
  agent,
@@ -32065,29 +32128,65 @@ async function executeSessionPrepare(publisher, socket, command, config2, direct
32065
32128
  sessionId: command.sessionId,
32066
32129
  signal
32067
32130
  };
32068
- const prepared = command.transient === true ? await sessions.inspectSession(preparation) : await sessions.prepareSession(preparation);
32131
+ const prepared = command.transient === true ? await sessions.inspectSession(preparation) : await sessions.prepareSession(preparation, metrics);
32132
+ const configuration = command.configuration;
32133
+ const configOptions = configuration === void 0 ? prepared.configOptions : await metrics.measure(
32134
+ "configuration",
32135
+ async () => await sessions.configureSession({
32136
+ ...preparation,
32137
+ booleanValue: configuration.booleanValue,
32138
+ configId: configuration.configId,
32139
+ requestId: command.requestId,
32140
+ value: configuration.value,
32141
+ valueType: configuration.valueType
32142
+ })
32143
+ );
32069
32144
  const listing = await directories.list(config2.bindingId, command.directoryId, signal);
32070
32145
  audit.record({
32071
32146
  commandId: command.requestId,
32072
32147
  event: "session_prepared",
32073
32148
  sessionId: command.sessionId
32074
32149
  });
32075
- publisher.recordRuntime(
32076
- command.sessionId,
32077
- encodeSessionReadyEvent({
32078
- additionalDirectories: resolved.additionalDirectoryEntries,
32079
- agentId: command.agentId,
32080
- agentName: prepared.agentName,
32081
- agentVersion: prepared.agentVersion,
32082
- capabilities: prepared.capabilities,
32083
- commands: prepared.commands,
32084
- configOptions: prepared.configOptions,
32085
- directoryId: command.directoryId,
32086
- directoryLabel: listing.directoryLabel,
32087
- kind: "session.ready",
32088
- requestId: command.requestId,
32089
- restored: prepared.restored,
32150
+ if (command.configuration !== void 0) {
32151
+ audit.record({
32152
+ commandId: command.requestId,
32153
+ event: "session_configured",
32090
32154
  sessionId: command.sessionId
32155
+ });
32156
+ publisher.recordRuntime(
32157
+ command.sessionId,
32158
+ encodeSessionConfigUpdatedEvent({
32159
+ configOptions,
32160
+ kind: "session.config.updated",
32161
+ requestId: command.requestId,
32162
+ sessionId: command.sessionId
32163
+ })
32164
+ );
32165
+ }
32166
+ const readyEvent = {
32167
+ additionalDirectories: resolved.additionalDirectoryEntries,
32168
+ agentId: command.agentId,
32169
+ agentName: prepared.agentName,
32170
+ agentVersion: prepared.agentVersion,
32171
+ capabilities: prepared.capabilities,
32172
+ commands: prepared.commands,
32173
+ configOptions,
32174
+ directoryId: command.directoryId,
32175
+ directoryLabel: listing.directoryLabel,
32176
+ kind: "session.ready",
32177
+ requestId: command.requestId,
32178
+ restored: prepared.restored,
32179
+ sessionId: command.sessionId,
32180
+ runtimeTimings: metrics.snapshot()
32181
+ };
32182
+ metrics.measureSync("readiness-commit", () => {
32183
+ publisher.recordRuntime(command.sessionId, encodeSessionReadyEvent(readyEvent));
32184
+ });
32185
+ send(
32186
+ socket,
32187
+ encodeSessionReadyEvent({
32188
+ ...readyEvent,
32189
+ runtimeTimings: metrics.snapshot()
32091
32190
  })
32092
32191
  );
32093
32192
  } catch (error62) {