@moxt-ai/mobius 0.0.156 → 0.0.157

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/index.js CHANGED
@@ -21515,8 +21515,15 @@ var acpSessionUpdateV2Schema = external_exports.discriminatedUnion("sessionUpdat
21515
21515
  }).readonly()
21516
21516
  ]);
21517
21517
  var lifecycleBase = { commandId: identifierStringSchema };
21518
- var sessionEventPayloadV2Schema = external_exports.discriminatedUnion("kind", [
21519
- external_exports.strictObject({ kind: external_exports.literal("acp.session.update"), update: acpSessionUpdateV2Schema }).readonly(),
21518
+ var lifecycleMetadataV2Schema = external_exports.discriminatedUnion("kind", [
21519
+ external_exports.strictObject({ kind: external_exports.literal("session.queue.changed") }).readonly(),
21520
+ external_exports.strictObject({ event: sessionPermissionRequestedEventSchema, kind: external_exports.literal("session.permission.requested") }).readonly(),
21521
+ external_exports.strictObject({ event: sessionPermissionResolvedEventSchema, kind: external_exports.literal("session.permission.resolved") }).readonly(),
21522
+ external_exports.strictObject({
21523
+ event: sessionElicitationRequestedEventSchema,
21524
+ kind: external_exports.literal("session.elicitation.requested")
21525
+ }).readonly(),
21526
+ external_exports.strictObject({ event: sessionElicitationResolvedEventSchema, kind: external_exports.literal("session.elicitation.resolved") }).readonly(),
21520
21527
  external_exports.strictObject({
21521
21528
  ...lifecycleBase,
21522
21529
  content: external_exports.array(contentBlockV2Schema).min(1).max(100).readonly(),
@@ -21538,6 +21545,10 @@ var sessionEventPayloadV2Schema = external_exports.discriminatedUnion("kind", [
21538
21545
  message: external_exports.string().min(1).max(500)
21539
21546
  }).readonly()
21540
21547
  ]);
21548
+ var sessionEventPayloadV2Schema = external_exports.discriminatedUnion("type", [
21549
+ external_exports.strictObject({ payload: acpSessionUpdateV2Schema, type: external_exports.literal("acp") }).readonly(),
21550
+ external_exports.strictObject({ payload: lifecycleMetadataV2Schema, type: external_exports.literal("lifecycle-metadata") }).readonly()
21551
+ ]);
21541
21552
  var positionSchema = external_exports.string().regex(/^(0|[1-9][0-9]*)$/);
21542
21553
  var provenanceSchema = external_exports.discriminatedUnion("kind", [
21543
21554
  external_exports.strictObject({ kind: external_exports.literal("original") }).readonly(),
@@ -21554,13 +21565,62 @@ var provenanceSchema = external_exports.discriminatedUnion("kind", [
21554
21565
  sourceSessionId: identifierStringSchema
21555
21566
  }).readonly()
21556
21567
  ]);
21557
- var sessionEventV2Schema = external_exports.strictObject({
21568
+ var sessionEventFieldsV2 = {
21558
21569
  eventId: identifierStringSchema,
21559
- payload: sessionEventPayloadV2Schema,
21560
21570
  position: positionSchema,
21561
21571
  provenance: provenanceSchema,
21562
21572
  recordedAt: external_exports.iso.datetime({ offset: true }),
21563
21573
  sessionId: identifierStringSchema
21574
+ };
21575
+ var sessionEventV2Schema = external_exports.discriminatedUnion("type", [
21576
+ external_exports.strictObject({ ...sessionEventFieldsV2, payload: acpSessionUpdateV2Schema, type: external_exports.literal("acp") }).readonly(),
21577
+ external_exports.strictObject({
21578
+ ...sessionEventFieldsV2,
21579
+ payload: lifecycleMetadataV2Schema,
21580
+ type: external_exports.literal("lifecycle-metadata")
21581
+ }).readonly()
21582
+ ]);
21583
+ var sessionExportV2Schema = external_exports.strictObject({
21584
+ binding: external_exports.strictObject({
21585
+ bindingId: identifierStringSchema,
21586
+ runtimeId: identifierStringSchema
21587
+ }).readonly(),
21588
+ events: external_exports.array(sessionEventV2Schema).max(MAX_EVENT_COUNT).readonly(),
21589
+ sessionId: identifierStringSchema,
21590
+ truncated: external_exports.boolean()
21591
+ }).readonly().superRefine((session, context) => {
21592
+ let previousPosition = 0n;
21593
+ const eventIds = /* @__PURE__ */ new Set();
21594
+ for (const [index2, event] of session.events.entries()) {
21595
+ if (event.sessionId !== session.sessionId) {
21596
+ context.addIssue({
21597
+ code: "custom",
21598
+ message: "Exported event belongs to a different Session",
21599
+ path: ["events", index2, "sessionId"]
21600
+ });
21601
+ }
21602
+ const position = BigInt(event.position);
21603
+ if (position <= previousPosition) {
21604
+ context.addIssue({
21605
+ code: "custom",
21606
+ message: "Exported events are not in increasing position order",
21607
+ path: ["events", index2, "position"]
21608
+ });
21609
+ }
21610
+ if (eventIds.has(event.eventId)) {
21611
+ context.addIssue({
21612
+ code: "custom",
21613
+ message: "Exported event identities are not unique",
21614
+ path: ["events", index2, "eventId"]
21615
+ });
21616
+ }
21617
+ previousPosition = position;
21618
+ eventIds.add(event.eventId);
21619
+ }
21620
+ });
21621
+ var mobiusSessionExportV2Schema = external_exports.strictObject({
21622
+ formatVersion: external_exports.literal("2"),
21623
+ session: sessionExportV2Schema
21564
21624
  }).readonly();
21565
21625
  var sessionEventEnvelopeV2Schema = external_exports.strictObject({ event: sessionEventV2Schema, kind: external_exports.literal(SESSION_EVENT_V2_KIND) }).readonly();
21566
21626
  var sessionEventLogPageV2Schema = external_exports.strictObject({
@@ -30858,8 +30918,15 @@ var MemoryAcpSessionStore = class {
30858
30918
  #sessions = /* @__PURE__ */ new Map();
30859
30919
  #deleted = /* @__PURE__ */ new Set();
30860
30920
  #eventsV2 = /* @__PURE__ */ new Map();
30861
- appendEventV2 = (event) => {
30862
- this.#eventsV2.set(event.sessionId, [...this.#eventsV2.get(event.sessionId) ?? [], event]);
30921
+ appendExecutingPromptEventV2 = (event) => {
30922
+ const events = this.#eventsV2.get(event.sessionId);
30923
+ if (events === void 0) {
30924
+ throw new Error("The executing prompt event log has not started");
30925
+ }
30926
+ events.push(event);
30927
+ };
30928
+ clearExecutingPromptEventsV2 = (sessionId) => {
30929
+ this.#eventsV2.delete(sessionId);
30863
30930
  };
30864
30931
  close = () => {
30865
30932
  this.#sessions.clear();
@@ -30890,7 +30957,12 @@ var MemoryAcpSessionStore = class {
30890
30957
  this.#sessions.delete(sessionId);
30891
30958
  this.#eventsV2.delete(sessionId);
30892
30959
  };
30893
- readEventsV2 = (sessionId) => this.#eventsV2.get(sessionId) ?? [];
30960
+ readExecutingPromptEventsV2 = (sessionId) => [
30961
+ ...this.#eventsV2.get(sessionId) ?? []
30962
+ ];
30963
+ startExecutingPromptEventsV2 = (sessionId, events) => {
30964
+ this.#eventsV2.set(sessionId, [...events]);
30965
+ };
30894
30966
  write = (session) => {
30895
30967
  this.#sessions.set(session.sessionId, session);
30896
30968
  };
@@ -30958,7 +31030,7 @@ var SqliteAcpSessionStore = class {
30958
31030
  close = () => {
30959
31031
  this.#sqliteDatabase.close();
30960
31032
  };
30961
- appendEventV2 = (event) => {
31033
+ appendExecutingPromptEventV2 = (event) => {
30962
31034
  this.#database.insert(sessionEventV2).values({
30963
31035
  eventId: event.eventId,
30964
31036
  eventJson: JSON.stringify(event),
@@ -30966,6 +31038,22 @@ var SqliteAcpSessionStore = class {
30966
31038
  sessionId: event.sessionId
30967
31039
  }).run();
30968
31040
  };
31041
+ clearExecutingPromptEventsV2 = (sessionId) => {
31042
+ this.#database.delete(sessionEventV2).where(eq(sessionEventV2.sessionId, sessionId)).run();
31043
+ };
31044
+ startExecutingPromptEventsV2 = (sessionId, events) => {
31045
+ this.#database.transaction((transaction) => {
31046
+ transaction.delete(sessionEventV2).where(eq(sessionEventV2.sessionId, sessionId)).run();
31047
+ for (const event of events) {
31048
+ transaction.insert(sessionEventV2).values({
31049
+ eventId: event.eventId,
31050
+ eventJson: JSON.stringify(event),
31051
+ position: Number(event.position),
31052
+ sessionId: event.sessionId
31053
+ }).run();
31054
+ }
31055
+ });
31056
+ };
30969
31057
  findNative = (agentId, nativeSessionId) => this.#database.select().from(acpSession).where(and(eq(acpSession.agentId, agentId), eq(acpSession.nativeSessionId, nativeSessionId))).all().map(decodeStoredSession);
30970
31058
  list = () => this.#database.select().from(acpSession).orderBy(desc(acpSession.updatedAt)).all().map(decodeStoredSession);
30971
31059
  markDeleted = (agentId, nativeSessionId) => {
@@ -30988,7 +31076,7 @@ var SqliteAcpSessionStore = class {
30988
31076
  const row = this.#database.select().from(acpSession).where(eq(acpSession.sessionId, sessionId)).get();
30989
31077
  return row === void 0 ? new MissingAcpStoredSession() : new FoundAcpStoredSession(decodeStoredSession(row));
30990
31078
  };
30991
- readEventsV2 = (sessionId) => this.#database.select({ eventJson: sessionEventV2.eventJson }).from(sessionEventV2).where(eq(sessionEventV2.sessionId, sessionId)).orderBy(asc(sessionEventV2.position)).all().map((row) => decodeSessionEventV2(row.eventJson));
31079
+ readExecutingPromptEventsV2 = (sessionId) => this.#database.select({ eventJson: sessionEventV2.eventJson }).from(sessionEventV2).where(eq(sessionEventV2.sessionId, sessionId)).orderBy(asc(sessionEventV2.position)).all().map((row) => decodeSessionEventV2(row.eventJson));
30992
31080
  remove = (sessionId) => {
30993
31081
  this.#database.transaction((transaction) => {
30994
31082
  transaction.delete(acpSession).where(eq(acpSession.sessionId, sessionId)).run();
@@ -31666,6 +31754,19 @@ var AgentSession = class _AgentSession {
31666
31754
  if (this.#turn.running) {
31667
31755
  throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
31668
31756
  }
31757
+ switch (this.#sessionLoad.kind) {
31758
+ case "not-requested":
31759
+ break;
31760
+ case "loaded":
31761
+ this.#sessionLoad = { kind: "not-requested" };
31762
+ break;
31763
+ case "capturing":
31764
+ throw new AcpExecutionError("Session history is still loading", {}, "session_busy");
31765
+ default: {
31766
+ const unexpected = this.#sessionLoad;
31767
+ throw new Error(`Unexpected Session load state: ${JSON.stringify(unexpected)}`);
31768
+ }
31769
+ }
31669
31770
  const prompt = this.buildPrompt(options);
31670
31771
  if (this.#sessionTitle === null || this.#sessionTitle.length === 0) {
31671
31772
  this.#sessionTitle = truncate2(options.prompt, 500);
@@ -31831,7 +31932,7 @@ var AgentSession = class _AgentSession {
31831
31932
  this.assertNativeSession(notification.sessionId);
31832
31933
  const update = notification.update;
31833
31934
  const rawUpdate = decodeAcpSessionUpdateV2(JSON.stringify(update));
31834
- if (this.#sessionLoad.kind !== "not-requested") {
31935
+ if (this.#sessionLoad.kind === "capturing") {
31835
31936
  await this.#sessionLoad.history.record(update, rawUpdate);
31836
31937
  } else {
31837
31938
  const onAcpUpdate = this.#getObserver().onAcpUpdate;
@@ -39238,7 +39339,7 @@ var DaemonAcpObserver = class {
39238
39339
  this.#recordV2 = recordV2;
39239
39340
  }
39240
39341
  onAcpUpdate = (sessionId, update) => {
39241
- this.#recordV2(sessionId, update);
39342
+ this.#recordV2(sessionId, { payload: update, type: "acp" });
39242
39343
  return Promise.resolve();
39243
39344
  };
39244
39345
  onCommands = (event) => {
@@ -39255,10 +39356,18 @@ var DaemonAcpObserver = class {
39255
39356
  };
39256
39357
  onElicitation = (event) => {
39257
39358
  this.#record(event.sessionId, event);
39359
+ this.#recordV2(event.sessionId, {
39360
+ payload: { event, kind: "session.elicitation.requested" },
39361
+ type: "lifecycle-metadata"
39362
+ });
39258
39363
  return Promise.resolve();
39259
39364
  };
39260
39365
  onElicitationResolved = (event) => {
39261
39366
  this.#record(event.sessionId, event);
39367
+ this.#recordV2(event.sessionId, {
39368
+ payload: { event, kind: "session.elicitation.resolved" },
39369
+ type: "lifecycle-metadata"
39370
+ });
39262
39371
  return Promise.resolve();
39263
39372
  };
39264
39373
  onInfo = (event) => {
@@ -39267,10 +39376,18 @@ var DaemonAcpObserver = class {
39267
39376
  };
39268
39377
  onPermission = (event) => {
39269
39378
  this.#record(event.sessionId, event);
39379
+ this.#recordV2(event.sessionId, {
39380
+ payload: { event, kind: "session.permission.requested" },
39381
+ type: "lifecycle-metadata"
39382
+ });
39270
39383
  return Promise.resolve();
39271
39384
  };
39272
39385
  onPermissionResolved = (event) => {
39273
39386
  this.#record(event.sessionId, event);
39387
+ this.#recordV2(event.sessionId, {
39388
+ payload: { event, kind: "session.permission.resolved" },
39389
+ type: "lifecycle-metadata"
39390
+ });
39274
39391
  return Promise.resolve();
39275
39392
  };
39276
39393
  onUsage = (event) => {
@@ -42511,25 +42628,16 @@ function replayEvent(sessionId, position, update) {
42511
42628
  const eventId = `native-${createHash6("sha256").update(sessionId).update(":").update(position.toString()).update(":").update(encoded).digest("hex")}`;
42512
42629
  return {
42513
42630
  eventId,
42514
- payload: { kind: "acp.session.update", update },
42631
+ payload: update,
42515
42632
  position: position.toString(),
42516
42633
  provenance: { kind: "original" },
42517
42634
  recordedAt: "1970-01-01T00:00:00.000Z",
42518
- sessionId
42635
+ sessionId,
42636
+ type: "acp"
42519
42637
  };
42520
42638
  }
42521
- async function executeSessionLiveHistoryV2PageLoad(socket, command, config2, directories, audit, sessions, localRecovery, executingPromptRequest, signal) {
42639
+ async function executeSessionLiveHistoryV2PageLoad(socket, command, config2, directories, audit, sessions, executingPromptRequest, signal) {
42522
42640
  try {
42523
- const local = localRecovery(command.sessionId, command.eventLimit, command.cursor);
42524
- if (local !== null) {
42525
- send(socket, {
42526
- events: local,
42527
- kind: "session.live-history.v2.page.loaded",
42528
- requestId: command.requestId,
42529
- sessionId: command.sessionId
42530
- });
42531
- return;
42532
- }
42533
42641
  const resolved = await resolveSessionDirectories(
42534
42642
  directories,
42535
42643
  config2.bindingId,
@@ -43413,7 +43521,10 @@ function createDaemonCommandHandler({
43413
43521
  timer,
43414
43522
  resolveEnvironment: resolveAgentCatalogEnvironment
43415
43523
  });
43416
- const queueChanged = (sessionId) => publisher.publish(sessionId, { kind: "session.queue.changed", sessionId });
43524
+ const queueChanged = (sessionId) => {
43525
+ publisher.publish(sessionId, { kind: "session.queue.changed", sessionId });
43526
+ eventLogV2.queueChanged(sessionId);
43527
+ };
43417
43528
  const activeTurnRecovery = (sessionId) => activeTurns.recovery((command) => command.sessionId === sessionId);
43418
43529
  const nativeRequestHandler = createNativeSessionRequestHandler({
43419
43530
  audit,
@@ -43463,7 +43574,6 @@ function createDaemonCommandHandler({
43463
43574
  directories,
43464
43575
  audit,
43465
43576
  sessions,
43466
- eventLogV2.recovery,
43467
43577
  eventLogV2.executingPromptRequest,
43468
43578
  signal
43469
43579
  );
@@ -44159,6 +44269,15 @@ var systemDaemonTimer = new SystemDaemonTimer();
44159
44269
 
44160
44270
  // src/session-event-log-v2.ts
44161
44271
  var EXECUTING_PROMPT_EVENT_LIMIT = 1e4;
44272
+ function eventArray(tail) {
44273
+ const reversed = [];
44274
+ let node2 = tail;
44275
+ while (node2 !== null) {
44276
+ reversed.push(node2.event);
44277
+ node2 = node2.previous;
44278
+ }
44279
+ return reversed.reverse();
44280
+ }
44162
44281
  function promptContent(command) {
44163
44282
  const content = [];
44164
44283
  if (command.prompt.length > 0) {
@@ -44182,27 +44301,8 @@ function promptContent(command) {
44182
44301
  }
44183
44302
  return content;
44184
44303
  }
44185
- function encodeLocalEventCursor(cursor) {
44186
- return Buffer.from(JSON.stringify(cursor), "utf8").toString("base64url");
44187
- }
44188
- function decodeLocalEventCursor(sessionId, cursor) {
44189
- try {
44190
- const value = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
44191
- if (typeof value !== "object" || value === null || Array.isArray(value) || Object.keys(value).sort().join(",") !== "endEvent,sessionId" || Reflect.get(value, "sessionId") !== sessionId) {
44192
- throw new Error("Invalid V2 event cursor target");
44193
- }
44194
- const endEvent = Reflect.get(value, "endEvent");
44195
- if (typeof endEvent !== "number" || !Number.isSafeInteger(endEvent) || endEvent < 0) {
44196
- throw new Error("Invalid V2 event cursor boundary");
44197
- }
44198
- return { endEvent, sessionId };
44199
- } catch (cause) {
44200
- throw new AcpExecutionError("The native Session event cursor is invalid", { cause }, "invalid_cursor");
44201
- }
44202
- }
44203
44304
  var DaemonSessionEventLogV2 = class {
44204
44305
  #positions = /* @__PURE__ */ new Map();
44205
- #events = /* @__PURE__ */ new Map();
44206
44306
  #requests = /* @__PURE__ */ new Map();
44207
44307
  #loadedRequestSessions = /* @__PURE__ */ new Set();
44208
44308
  #publisher;
@@ -44211,13 +44311,8 @@ var DaemonSessionEventLogV2 = class {
44211
44311
  this.#publisher = publisher;
44212
44312
  this.#store = store;
44213
44313
  }
44214
- #eventsFor = (sessionId) => {
44215
- const cached2 = this.#events.get(sessionId);
44216
- if (cached2 !== void 0) {
44217
- return cached2;
44218
- }
44219
- const stored = this.#store.readEventsV2(sessionId);
44220
- this.#events.set(sessionId, stored);
44314
+ #loadStoredEvents = (sessionId) => {
44315
+ const stored = this.#store.readExecutingPromptEventsV2(sessionId);
44221
44316
  const lastPosition = stored.at(-1)?.position;
44222
44317
  if (lastPosition !== void 0) {
44223
44318
  this.#positions.set(sessionId, Number(lastPosition));
@@ -44228,25 +44323,37 @@ var DaemonSessionEventLogV2 = class {
44228
44323
  if (this.#loadedRequestSessions.has(sessionId)) {
44229
44324
  return;
44230
44325
  }
44231
- for (const event of this.#eventsFor(sessionId)) {
44326
+ for (const event of this.#loadStoredEvents(sessionId)) {
44327
+ if (event.type === "acp") {
44328
+ const executing = [...this.#requests.values()].find(
44329
+ (request) => request.sessionId === sessionId && request.kind === "executing"
44330
+ );
44331
+ if (executing !== void 0) {
44332
+ this.#requests.set(executing.commandId, {
44333
+ ...executing,
44334
+ eventTail: { event, previous: executing.eventTail }
44335
+ });
44336
+ }
44337
+ continue;
44338
+ }
44232
44339
  const payload = event.payload;
44233
44340
  if (payload.kind === "prompt.requested") {
44234
44341
  this.#requests.set(payload.commandId, {
44235
44342
  commandId: payload.commandId,
44236
- events: [event],
44343
+ eventTail: { event, previous: null },
44237
44344
  kind: "requested",
44238
44345
  sessionId
44239
44346
  });
44240
44347
  continue;
44241
44348
  }
44242
- if (payload.kind === "acp.session.update") {
44349
+ if ("event" in payload) {
44243
44350
  const executing = [...this.#requests.values()].find(
44244
44351
  (request) => request.sessionId === sessionId && request.kind === "executing"
44245
44352
  );
44246
44353
  if (executing !== void 0) {
44247
44354
  this.#requests.set(executing.commandId, {
44248
44355
  ...executing,
44249
- events: [...executing.events, event]
44356
+ eventTail: { event, previous: executing.eventTail }
44250
44357
  });
44251
44358
  }
44252
44359
  continue;
@@ -44258,13 +44365,12 @@ var DaemonSessionEventLogV2 = class {
44258
44365
  if (state === void 0) {
44259
44366
  continue;
44260
44367
  }
44261
- const events = [...state.events, event];
44262
44368
  if (payload.kind === "prompt.started") {
44263
44369
  this.#requests.set(payload.commandId, {
44264
44370
  attemptId: payload.attemptId,
44265
44371
  commandId: payload.commandId,
44266
44372
  epoch: payload.epoch,
44267
- events,
44373
+ eventTail: { event, previous: state.eventTail },
44268
44374
  kind: "executing",
44269
44375
  sessionId
44270
44376
  });
@@ -44275,31 +44381,41 @@ var DaemonSessionEventLogV2 = class {
44275
44381
  this.#loadedRequestSessions.add(sessionId);
44276
44382
  };
44277
44383
  #append = (sessionId, payload) => {
44278
- this.#eventsFor(sessionId);
44384
+ this.#loadRequests(sessionId);
44279
44385
  const position = (this.#positions.get(sessionId) ?? 0) + 1;
44280
44386
  const event = {
44281
44387
  eventId: `event-${crypto.randomUUID()}`,
44282
- payload,
44388
+ ...payload,
44283
44389
  position: position.toString(),
44284
44390
  provenance: { kind: "original" },
44285
44391
  recordedAt: (/* @__PURE__ */ new Date()).toISOString(),
44286
44392
  sessionId
44287
44393
  };
44288
- this.#store.appendEventV2(event);
44289
44394
  this.#positions.set(sessionId, position);
44290
- this.#events.set(sessionId, [...this.#eventsFor(sessionId), event]);
44291
44395
  this.#publisher.publishV2(event);
44292
44396
  return event;
44293
44397
  };
44294
44398
  requested = (command) => {
44399
+ const content = promptContent(command);
44295
44400
  const event = this.#append(command.sessionId, {
44296
- commandId: command.commandId,
44297
- content: promptContent(command),
44298
- kind: "prompt.requested"
44401
+ payload: { commandId: command.commandId, content, kind: "prompt.requested" },
44402
+ type: "lifecycle-metadata"
44299
44403
  });
44404
+ let eventTail = { event, previous: null };
44405
+ for (const block of content) {
44406
+ const userMessageEvent = this.#append(command.sessionId, {
44407
+ payload: {
44408
+ content: block,
44409
+ messageId: command.commandId,
44410
+ sessionUpdate: "user_message_chunk"
44411
+ },
44412
+ type: "acp"
44413
+ });
44414
+ eventTail = { event: userMessageEvent, previous: eventTail };
44415
+ }
44300
44416
  this.#requests.set(command.commandId, {
44301
44417
  commandId: command.commandId,
44302
- events: [event],
44418
+ eventTail,
44303
44419
  kind: "requested",
44304
44420
  sessionId: command.sessionId
44305
44421
  });
@@ -44310,39 +44426,85 @@ var DaemonSessionEventLogV2 = class {
44310
44426
  if (request?.sessionId !== sessionId) {
44311
44427
  return;
44312
44428
  }
44313
- const event = this.#append(sessionId, { attemptId, commandId, epoch, kind: "prompt.started" });
44429
+ const event = this.#append(sessionId, {
44430
+ payload: { attemptId, commandId, epoch, kind: "prompt.started" },
44431
+ type: "lifecycle-metadata"
44432
+ });
44314
44433
  this.#requests.set(commandId, {
44315
44434
  attemptId,
44316
44435
  commandId,
44317
44436
  epoch,
44318
- events: [...request.events, event],
44437
+ eventTail: { event, previous: request.eventTail },
44319
44438
  kind: "executing",
44320
44439
  sessionId
44321
44440
  });
44441
+ this.#store.startExecutingPromptEventsV2(sessionId, eventArray({ event, previous: request.eventTail }));
44322
44442
  };
44323
- acpUpdate = (sessionId, update) => {
44443
+ record = (sessionId, payload) => {
44324
44444
  this.#loadRequests(sessionId);
44325
- const event = this.#append(sessionId, { kind: "acp.session.update", update });
44445
+ const event = this.#append(sessionId, payload);
44446
+ this.#appendToExecutingRequest(sessionId, event);
44447
+ };
44448
+ #appendToExecutingRequest = (sessionId, event) => {
44326
44449
  const executing = [...this.#requests.values()].find(
44327
44450
  (request) => request.sessionId === sessionId && request.kind === "executing"
44328
44451
  );
44329
44452
  if (executing !== void 0) {
44330
- this.#requests.set(executing.commandId, { ...executing, events: [...executing.events, event] });
44453
+ this.#requests.set(executing.commandId, {
44454
+ ...executing,
44455
+ eventTail: { event, previous: executing.eventTail }
44456
+ });
44457
+ this.#store.appendExecutingPromptEventV2(event);
44331
44458
  }
44332
44459
  };
44460
+ queueChanged = (sessionId) => {
44461
+ const event = this.#append(sessionId, {
44462
+ payload: { kind: "session.queue.changed" },
44463
+ type: "lifecycle-metadata"
44464
+ });
44465
+ this.#appendToExecutingRequest(sessionId, event);
44466
+ };
44333
44467
  completed = (sessionId, commandId, stopReason) => {
44334
- this.#append(sessionId, { commandId, kind: "prompt.completed", stopReason });
44468
+ this.#loadRequests(sessionId);
44469
+ const request = this.#requests.get(commandId);
44470
+ const event = this.#append(sessionId, {
44471
+ payload: { commandId, kind: "prompt.completed", stopReason },
44472
+ type: "lifecycle-metadata"
44473
+ });
44335
44474
  this.#requests.delete(commandId);
44475
+ if (request?.kind === "executing" && request.sessionId === sessionId) {
44476
+ this.#store.clearExecutingPromptEventsV2(sessionId);
44477
+ } else {
44478
+ this.#appendToExecutingRequest(sessionId, event);
44479
+ }
44336
44480
  };
44337
44481
  failed = (sessionId, commandId, code, message) => {
44338
44482
  this.#loadRequests(sessionId);
44339
44483
  const request = this.#requests.get(commandId);
44484
+ let event;
44340
44485
  if (request?.kind === "executing" && request.commandId === commandId) {
44341
- this.#append(sessionId, { attemptId: request.attemptId, code, commandId, kind: "prompt.failed", message });
44486
+ event = this.#append(sessionId, {
44487
+ payload: {
44488
+ attemptId: request.attemptId,
44489
+ code,
44490
+ commandId,
44491
+ kind: "prompt.failed",
44492
+ message
44493
+ },
44494
+ type: "lifecycle-metadata"
44495
+ });
44342
44496
  } else {
44343
- this.#append(sessionId, { code, commandId, kind: "prompt.failed-before-start", message });
44497
+ event = this.#append(sessionId, {
44498
+ payload: { code, commandId, kind: "prompt.failed-before-start", message },
44499
+ type: "lifecycle-metadata"
44500
+ });
44344
44501
  }
44345
44502
  this.#requests.delete(commandId);
44503
+ if (request?.kind === "executing" && request.sessionId === sessionId) {
44504
+ this.#store.clearExecutingPromptEventsV2(sessionId);
44505
+ } else {
44506
+ this.#appendToExecutingRequest(sessionId, event);
44507
+ }
44346
44508
  };
44347
44509
  executingPromptRequest = (sessionId) => {
44348
44510
  this.#loadRequests(sessionId);
@@ -44352,42 +44514,15 @@ var DaemonSessionEventLogV2 = class {
44352
44514
  if (request?.kind !== "executing") {
44353
44515
  return null;
44354
44516
  }
44355
- const events = request.events.slice(-EXECUTING_PROMPT_EVENT_LIMIT);
44517
+ const requestEvents = eventArray(request.eventTail);
44518
+ const events = requestEvents.slice(-EXECUTING_PROMPT_EVENT_LIMIT);
44356
44519
  return {
44357
44520
  attemptId: request.attemptId,
44358
44521
  commandId: request.commandId,
44359
44522
  epoch: request.epoch,
44360
44523
  events,
44361
44524
  tailCursor: events.at(-1)?.eventId ?? "",
44362
- truncated: events.length < request.events.length
44363
- };
44364
- };
44365
- recovery = (sessionId, eventLimit, cursor) => {
44366
- const events = this.#eventsFor(sessionId);
44367
- if (events.length === 0) {
44368
- return null;
44369
- }
44370
- const executingPromptRequest = this.executingPromptRequest(sessionId);
44371
- const executingRequestState = [...this.#requests.values()].find(
44372
- (request) => request.sessionId === sessionId && request.kind === "executing"
44373
- );
44374
- const firstExecutingPosition = executingRequestState?.events[0]?.position;
44375
- const historyEvents = firstExecutingPosition === void 0 ? events : events.filter((event) => BigInt(event.position) < BigInt(firstExecutingPosition));
44376
- const endEvent = cursor === null ? historyEvents.length : decodeLocalEventCursor(sessionId, cursor).endEvent;
44377
- if (endEvent > historyEvents.length) {
44378
- throw new AcpExecutionError("The native Session event cursor is invalid", {}, "invalid_cursor");
44379
- }
44380
- const startEvent = Math.max(0, endEvent - eventLimit);
44381
- const pageEvents = historyEvents.slice(startEvent, endEvent);
44382
- return {
44383
- executingPromptRequest,
44384
- history: {
44385
- events: pageEvents,
44386
- nextCursor: startEvent === 0 ? null : encodeLocalEventCursor({ endEvent: startEvent, sessionId }),
44387
- tailCursor: pageEvents.at(-1)?.eventId ?? "",
44388
- truncated: startEvent > 0,
44389
- version: 2
44390
- }
44525
+ truncated: events.length < requestEvents.length
44391
44526
  };
44392
44527
  };
44393
44528
  };
@@ -44469,7 +44604,7 @@ async function runDaemon(config2, audit, signal, timer = systemDaemonTimer, opti
44469
44604
  publisher.publish(sessionId, activeTurns.recordEvent(event));
44470
44605
  };
44471
44606
  const agentRuntime = new AgentRuntimeManager(
44472
- new DaemonAcpObserver(recordSessionEvent, eventLogV2.acpUpdate),
44607
+ new DaemonAcpObserver(recordSessionEvent, eventLogV2.record),
44473
44608
  sessionStore,
44474
44609
  {
44475
44610
  read: async (options2, nativeSessionId, nativeTurn) => options2.agent.id === "codex" ? (await readCodexFullHistory(