@moxt-ai/mobius 0.0.77 → 0.0.79

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
@@ -21256,6 +21256,8 @@ var encodeRecordedSessionEvent = (event) => JSON.stringify(decodeRecordedSession
21256
21256
  // ../protocol/src/session-live-history.ts
21257
21257
  var SESSION_LIVE_HISTORY_LOAD_KIND = "session.live-history.load";
21258
21258
  var SESSION_LIVE_HISTORY_LOADED_KIND = "session.live-history.loaded";
21259
+ var SESSION_LIVE_HISTORY_PAGE_LOAD_KIND = "session.live-history.page.load";
21260
+ var SESSION_LIVE_HISTORY_PAGE_LOADED_KIND = "session.live-history.page.loaded";
21259
21261
  var identifier2 = external_exports.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/);
21260
21262
  var directoryIdentifier = external_exports.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,2047}$/);
21261
21263
  var sessionLiveHistoryLoadSchema = external_exports.strictObject({
@@ -21273,6 +21275,23 @@ var sessionLiveHistoryLoadedSchema = external_exports.strictObject({
21273
21275
  requestId: identifier2,
21274
21276
  sessionId: identifier2
21275
21277
  }).readonly();
21278
+ var sessionLiveHistoryPageLoadSchema = external_exports.strictObject({
21279
+ additionalDirectoryIds: external_exports.array(directoryIdentifier).max(16).readonly(),
21280
+ agentId: identifier2,
21281
+ bindingId: identifier2,
21282
+ cursor: external_exports.string().min(1).max(512).nullable(),
21283
+ directoryId: directoryIdentifier,
21284
+ kind: external_exports.literal(SESSION_LIVE_HISTORY_PAGE_LOAD_KIND),
21285
+ limit: external_exports.number().int().min(1).max(100),
21286
+ requestId: identifier2,
21287
+ sessionId: identifier2
21288
+ }).readonly();
21289
+ var sessionLiveHistoryPageLoadedSchema = external_exports.strictObject({
21290
+ kind: external_exports.literal(SESSION_LIVE_HISTORY_PAGE_LOADED_KIND),
21291
+ page: nativeMessagePageSchema,
21292
+ requestId: identifier2,
21293
+ sessionId: identifier2
21294
+ }).readonly();
21276
21295
  function decode6(schema, encoded) {
21277
21296
  try {
21278
21297
  const value = JSON.parse(encoded);
@@ -21285,6 +21304,10 @@ var decodeSessionLiveHistoryLoad = (encoded) => decode6(sessionLiveHistoryLoadSc
21285
21304
  var decodeSessionLiveHistoryLoaded = (encoded) => decode6(sessionLiveHistoryLoadedSchema, encoded);
21286
21305
  var encodeSessionLiveHistoryLoad = (value) => JSON.stringify(sessionLiveHistoryLoadSchema.parse(value));
21287
21306
  var encodeSessionLiveHistoryLoaded = (value) => JSON.stringify(sessionLiveHistoryLoadedSchema.parse(value));
21307
+ var decodeSessionLiveHistoryPageLoad = (encoded) => decode6(sessionLiveHistoryPageLoadSchema, encoded);
21308
+ var decodeSessionLiveHistoryPageLoaded = (encoded) => decode6(sessionLiveHistoryPageLoadedSchema, encoded);
21309
+ var encodeSessionLiveHistoryPageLoad = (value) => JSON.stringify(sessionLiveHistoryPageLoadSchema.parse(value));
21310
+ var encodeSessionLiveHistoryPageLoaded = (value) => JSON.stringify(sessionLiveHistoryPageLoadedSchema.parse(value));
21288
21311
 
21289
21312
  // ../protocol/src/daemon-transport.ts
21290
21313
  function defineMessageSignature(kind, encoder, decoder) {
@@ -21388,6 +21411,11 @@ var serviceToDaemonSignatures = {
21388
21411
  encodeSessionLiveHistoryLoad,
21389
21412
  decodeSessionLiveHistoryLoad
21390
21413
  ),
21414
+ [SESSION_LIVE_HISTORY_PAGE_LOAD_KIND]: defineMessageSignature(
21415
+ SESSION_LIVE_HISTORY_PAGE_LOAD_KIND,
21416
+ encodeSessionLiveHistoryPageLoad,
21417
+ decodeSessionLiveHistoryPageLoad
21418
+ ),
21391
21419
  [SESSION_PERMISSION_RESPOND_KIND]: defineMessageSignature(
21392
21420
  SESSION_PERMISSION_RESPOND_KIND,
21393
21421
  encodeSessionPermissionRespondCommand,
@@ -21498,6 +21526,11 @@ var daemonToServiceSignatures = {
21498
21526
  encodeSessionLiveHistoryLoaded,
21499
21527
  decodeSessionLiveHistoryLoaded
21500
21528
  ),
21529
+ [SESSION_LIVE_HISTORY_PAGE_LOADED_KIND]: defineMessageSignature(
21530
+ SESSION_LIVE_HISTORY_PAGE_LOADED_KIND,
21531
+ encodeSessionLiveHistoryPageLoaded,
21532
+ decodeSessionLiveHistoryPageLoaded
21533
+ ),
21501
21534
  [SESSION_READY_KIND]: defineMessageSignature(SESSION_READY_KIND, encodeSessionReadyEvent, decodeSessionReadyEvent),
21502
21535
  [WORKSPACE_ENTRIES_KIND]: defineMessageSignature(
21503
21536
  WORKSPACE_ENTRIES_KIND,
@@ -30876,6 +30909,29 @@ function sessionConfigProtocolForAgent(agentId) {
30876
30909
  }
30877
30910
 
30878
30911
  // src/acp/session-history.ts
30912
+ var HISTORY_ACTIVITY_FIELD_LIMIT = 1e5;
30913
+ var HISTORY_ACTIVITY_LIMIT = 100;
30914
+ function historyActivity(activity2) {
30915
+ return {
30916
+ activityId: activity2.activityId.slice(0, 128),
30917
+ activityType: activity2.activityType.slice(0, 128),
30918
+ content: activity2.content.slice(0, 100),
30919
+ input: activity2.input.slice(0, HISTORY_ACTIVITY_FIELD_LIMIT),
30920
+ locations: activity2.locations.slice(0, 100).map((location) => location.slice(0, 2048)),
30921
+ name: activity2.name.slice(0, 128),
30922
+ output: activity2.output.slice(0, HISTORY_ACTIVITY_FIELD_LIMIT),
30923
+ sequence: activity2.sequence,
30924
+ status: activity2.status.slice(0, 64),
30925
+ title: activity2.title.slice(0, 500),
30926
+ toolKind: activity2.toolKind.slice(0, 64)
30927
+ };
30928
+ }
30929
+ function historyContentCharacters(messages) {
30930
+ return messages.reduce(
30931
+ (total, message) => total + message.text.length + (message.role === "assistant" ? JSON.stringify(message.activities).length + JSON.stringify(message.content).length : 0),
30932
+ 0
30933
+ );
30934
+ }
30879
30935
  var AcpHistoryReplay = class {
30880
30936
  #activities = /* @__PURE__ */ new Map();
30881
30937
  #messages = [];
@@ -30982,33 +31038,45 @@ var AcpHistoryReplay = class {
30982
31038
  };
30983
31039
  snapshot = async () => {
30984
31040
  await this.#activityTracker.finish();
31041
+ const messages = this.#messages.map((message) => {
31042
+ if (message.role === "user") {
31043
+ return message;
31044
+ }
31045
+ const activities = message.activityIds.flatMap((id) => {
31046
+ const activity2 = this.#activities.get(id);
31047
+ return activity2 === void 0 ? [] : [historyActivity(activity2)];
31048
+ });
31049
+ if (activities.length > HISTORY_ACTIVITY_LIMIT) {
31050
+ this.#truncated = true;
31051
+ }
31052
+ return {
31053
+ role: "assistant",
31054
+ text: message.text,
31055
+ content: message.content.slice(0, 100),
31056
+ activities: activities.slice(-HISTORY_ACTIVITY_LIMIT)
31057
+ };
31058
+ });
31059
+ while (historyContentCharacters(messages) > SESSION_HISTORY_CHARACTER_LIMIT) {
31060
+ const richIndex = messages.findIndex(
31061
+ (message) => message.role === "assistant" && (message.activities.length > 0 || message.content.length > 0)
31062
+ );
31063
+ if (richIndex === -1) {
31064
+ messages.shift();
31065
+ } else {
31066
+ const message = messages[richIndex];
31067
+ if (message?.role === "assistant") {
31068
+ messages[richIndex] = {
31069
+ ...message,
31070
+ activities: message.activities.length > 0 ? message.activities.slice(1) : message.activities,
31071
+ content: message.activities.length === 0 && message.content.length > 0 ? message.content.slice(1) : message.content
31072
+ };
31073
+ }
31074
+ }
31075
+ this.#truncated = true;
31076
+ }
30985
31077
  return {
30986
31078
  status: "available",
30987
- messages: this.#messages.map(
30988
- (message) => message.role === "user" ? message : {
30989
- role: "assistant",
30990
- text: message.text,
30991
- content: message.content,
30992
- activities: message.activityIds.flatMap((id) => {
30993
- const activity2 = this.#activities.get(id);
30994
- return activity2 === void 0 ? [] : [
30995
- {
30996
- activityId: activity2.activityId,
30997
- activityType: activity2.activityType,
30998
- content: activity2.content,
30999
- input: activity2.input,
31000
- locations: activity2.locations,
31001
- name: activity2.name,
31002
- output: activity2.output,
31003
- sequence: activity2.sequence,
31004
- status: activity2.status,
31005
- title: activity2.title,
31006
- toolKind: activity2.toolKind
31007
- }
31008
- ];
31009
- })
31010
- }
31011
- ),
31079
+ messages,
31012
31080
  truncated: this.#truncated,
31013
31081
  coveredCommandIds: [],
31014
31082
  coveredMessageIds: []
@@ -34968,6 +35036,14 @@ var modelListResponseSchema = external_exports.object({
34968
35036
  data: external_exports.array(modelSchema).max(1e3),
34969
35037
  nextCursor: boundedText(1024).nullish()
34970
35038
  });
35039
+ var threadSettingsUpdatedSchema = external_exports.object({
35040
+ threadId: boundedText(256),
35041
+ threadSettings: external_exports.object({
35042
+ collaborationMode: external_exports.object({
35043
+ mode: external_exports.enum(["default", "plan"])
35044
+ }).nullish()
35045
+ })
35046
+ });
34971
35047
 
34972
35048
  // src/acp/codex/app-server-client.ts
34973
35049
  var INITIALIZE_TIMEOUT_MILLISECONDS = 3e4;
@@ -35152,7 +35228,10 @@ var CodexAppServerClient = class {
35152
35228
  initialize = async (signal) => {
35153
35229
  await this.request(
35154
35230
  "initialize",
35155
- { clientInfo: { name: "mobius", version: "1.0.0" } },
35231
+ {
35232
+ capabilities: { experimentalApi: true },
35233
+ clientInfo: { name: "mobius", version: "1.0.0" }
35234
+ },
35156
35235
  AbortSignal.any([signal, AbortSignal.timeout(INITIALIZE_TIMEOUT_MILLISECONDS)])
35157
35236
  );
35158
35237
  this.notify("initialized", void 0);
@@ -35312,6 +35391,7 @@ var MODEL_SETTING_ID = "model";
35312
35391
  var REASONING_EFFORT_SETTING_ID = "reasoning_effort";
35313
35392
  var MODE_SETTING_ID = "mode";
35314
35393
  var FAST_MODE_SETTING_ID = "fast-mode";
35394
+ var COLLABORATION_MODE_SETTING_ID = "collaboration_mode";
35315
35395
  var MODEL_CATALOG_LIMIT = 1e3;
35316
35396
  var FAST_TIER = "fast";
35317
35397
  var SERVICE_TIER_FAST = "priority";
@@ -35441,6 +35521,22 @@ function modeSetting(currentValue) {
35441
35521
  type: "select"
35442
35522
  };
35443
35523
  }
35524
+ var COLLABORATION_MODES = Object.freeze([
35525
+ { description: "Codex answers and makes changes directly", name: "Build", value: "default" },
35526
+ { description: "Codex plans before making changes", name: "Plan", value: "plan" }
35527
+ ]);
35528
+ function collaborationModeSetting(currentValue) {
35529
+ const current = COLLABORATION_MODES.some((mode) => mode.value === currentValue) ? currentValue : COLLABORATION_MODES[0]?.value ?? "default";
35530
+ return {
35531
+ category: "collaboration_mode",
35532
+ currentValue: current,
35533
+ description: "How Codex collaborates for subsequent turns",
35534
+ id: COLLABORATION_MODE_SETTING_ID,
35535
+ name: "Collaboration mode",
35536
+ options: [...COLLABORATION_MODES],
35537
+ type: "select"
35538
+ };
35539
+ }
35444
35540
  function fastModeSetting(model, enabled) {
35445
35541
  if (model?.additionalSpeedTiers?.includes(FAST_TIER) !== true) {
35446
35542
  return null;
@@ -35502,11 +35598,19 @@ var CodexAcpAgent = class {
35502
35598
  // Whether the current turn must carry a policy at all. Until the user picks a mode, Codex's own
35503
35599
  // configuration decides, so `turn/start` omits the fields and inherits what the host set up.
35504
35600
  #policySelected = false;
35601
+ // Whether a collaboration mode choice has been made in this session. A restored thread keeps
35602
+ // the mode Codex already persisted, so the first turn does not reset it to Build.
35603
+ #collaborationModeSelected = false;
35604
+ // Whether the app-server accepts collaboration mode on `turn/start`, probed once per
35605
+ // app-server. An older Codex rejects the experimental field, and the setting must disappear
35606
+ // rather than break every turn.
35607
+ #collaborationModeSupported = null;
35505
35608
  #selection;
35506
35609
  #turn = null;
35507
35610
  #updateTail = Promise.resolve();
35508
35611
  constructor(options) {
35509
35612
  this.#selection = {
35613
+ collaborationMode: "default",
35510
35614
  effort: "",
35511
35615
  fastMode: false,
35512
35616
  mode: initialMode(options.environment),
@@ -35547,9 +35651,12 @@ var CodexAcpAgent = class {
35547
35651
  this.#nativeSessionId = created.thread.id;
35548
35652
  this.#selection = {
35549
35653
  ...this.#selection,
35654
+ collaborationMode: "default",
35550
35655
  effort: created.reasoningEffort ?? "",
35551
35656
  model: created.model ?? ""
35552
35657
  };
35658
+ this.#collaborationModeSelected = false;
35659
+ await this.#probeCollaborationMode(context.signal);
35553
35660
  return { configOptions: this.#configOptions(), sessionId: created.thread.id };
35554
35661
  }).onRequest(methods4.agent.session.setConfigOption, async (context) => {
35555
35662
  this.#applySelection(context.params.configId, context.params.value);
@@ -35566,8 +35673,9 @@ var CodexAcpAgent = class {
35566
35673
  )
35567
35674
  );
35568
35675
  this.#selectRestoredThread(resumed);
35676
+ await this.#probeCollaborationMode(context.signal);
35569
35677
  await this.#replayHistory(resumed.turns);
35570
- return {};
35678
+ return { configOptions: this.#configOptions() };
35571
35679
  }).onRequest(methods4.agent.session.fork, async (context) => {
35572
35680
  const forked = threadForkResponseSchema.parse(
35573
35681
  await this.#appServer.request(
@@ -35581,8 +35689,9 @@ var CodexAcpAgent = class {
35581
35689
  )
35582
35690
  );
35583
35691
  this.#selectRestoredThread(forked);
35692
+ await this.#probeCollaborationMode(context.signal);
35584
35693
  await this.#replayHistory(forked.turns);
35585
- return { sessionId: forked.thread.id };
35694
+ return { configOptions: this.#configOptions(), sessionId: forked.thread.id };
35586
35695
  }).onRequest(methods4.agent.session.resume, async (context) => {
35587
35696
  const resumed = threadResumeResponseSchema.parse(
35588
35697
  await this.#appServer.request(
@@ -35592,7 +35701,8 @@ var CodexAcpAgent = class {
35592
35701
  )
35593
35702
  );
35594
35703
  this.#selectRestoredThread(resumed);
35595
- return {};
35704
+ await this.#probeCollaborationMode(context.signal);
35705
+ return { configOptions: this.#configOptions() };
35596
35706
  }).onRequest(methods4.agent.session.prompt, async (context) => {
35597
35707
  return await this.#runTurn(promptInput(context.params.prompt), context.signal);
35598
35708
  }).onRequest(methods4.agent.session.list, async (context) => {
@@ -35664,6 +35774,27 @@ var CodexAcpAgent = class {
35664
35774
  this.#models = Object.freeze(models.slice(0, MODEL_CATALOG_LIMIT));
35665
35775
  };
35666
35776
  #model = (id) => this.#models.find((model) => model.id === id);
35777
+ /**
35778
+ * Learns, once per app-server, whether collaboration mode can be applied. An empty settings
35779
+ * update is a no-op on a supporting Codex; an older Codex answers an error, which costs the
35780
+ * Build/Plan selector alone without changing a restored thread's persisted mode.
35781
+ */
35782
+ #probeCollaborationMode = async (signal) => {
35783
+ if (this.#collaborationModeSupported !== null) {
35784
+ return;
35785
+ }
35786
+ try {
35787
+ await this.#appServer.request(
35788
+ "thread/settings/update",
35789
+ { threadId: this.#nativeSessionId },
35790
+ AbortSignal.any([signal, AbortSignal.timeout(APPROVAL_TIMEOUT_MILLISECONDS)])
35791
+ );
35792
+ this.#collaborationModeSupported = true;
35793
+ } catch {
35794
+ signal.throwIfAborted();
35795
+ this.#collaborationModeSupported = false;
35796
+ }
35797
+ };
35667
35798
  /** The ACP settings Mobius discovers and renders in Run settings. */
35668
35799
  #configOptions = () => {
35669
35800
  const options = [];
@@ -35679,6 +35810,9 @@ var CodexAcpAgent = class {
35679
35810
  if (fastMode !== null) {
35680
35811
  options.push(fastMode);
35681
35812
  }
35813
+ if (this.#collaborationModeSupported === true) {
35814
+ options.push(collaborationModeSetting(this.#selection.collaborationMode));
35815
+ }
35682
35816
  options.push(modeSetting(this.#selection.mode));
35683
35817
  return options;
35684
35818
  };
@@ -35771,6 +35905,18 @@ var CodexAcpAgent = class {
35771
35905
  this.#policySelected = true;
35772
35906
  return;
35773
35907
  }
35908
+ if (configId === COLLABORATION_MODE_SETTING_ID) {
35909
+ if (!COLLABORATION_MODES.some((mode) => mode.value === value)) {
35910
+ throw new AcpExecutionError(
35911
+ "The requested Codex collaboration mode is unavailable",
35912
+ {},
35913
+ "invalid_session_configuration"
35914
+ );
35915
+ }
35916
+ this.#selection = { ...this.#selection, collaborationMode: value };
35917
+ this.#collaborationModeSelected = true;
35918
+ return;
35919
+ }
35774
35920
  if (configId === MODEL_SETTING_ID) {
35775
35921
  if (value === this.#selection.model) {
35776
35922
  return;
@@ -35819,6 +35965,17 @@ var CodexAcpAgent = class {
35819
35965
  update
35820
35966
  });
35821
35967
  };
35968
+ /** Pushes the current settings so a mode change Codex applied reaches the Run settings panel. */
35969
+ #emitConfigOptionsUpdate = async () => {
35970
+ const connection = this.#connection;
35971
+ if (connection === null || this.#nativeSessionId.length === 0) {
35972
+ return;
35973
+ }
35974
+ await connection.client.notify(methods4.client.session.update, {
35975
+ sessionId: this.#nativeSessionId,
35976
+ update: { configOptions: this.#configOptions(), sessionUpdate: "config_option_update" }
35977
+ });
35978
+ };
35822
35979
  // Notifications are serialized so two app-server items cannot interleave into one ACP update
35823
35980
  // stream, and the caller awaits the result: `turn/completed` must not resolve the prompt before
35824
35981
  // the updates it precedes have reached the client.
@@ -35851,6 +36008,19 @@ var CodexAcpAgent = class {
35851
36008
  }
35852
36009
  return;
35853
36010
  }
36011
+ if (method === "thread/settings/updated") {
36012
+ const decoded = threadSettingsUpdatedSchema.safeParse(params);
36013
+ if (decoded.success && decoded.data.threadId === this.#nativeSessionId) {
36014
+ const mode = decoded.data.threadSettings.collaborationMode?.mode;
36015
+ if (mode !== void 0 && mode !== this.#selection.collaborationMode) {
36016
+ this.#selection = { ...this.#selection, collaborationMode: mode };
36017
+ await this.#enqueue(async () => {
36018
+ await this.#emitConfigOptionsUpdate();
36019
+ });
36020
+ }
36021
+ }
36022
+ return;
36023
+ }
35854
36024
  if (method === "turn/plan/updated") {
35855
36025
  const plan = params.plan;
35856
36026
  if (Array.isArray(plan) && params.threadId === this.#nativeSessionId) {
@@ -36029,6 +36199,19 @@ var CodexAcpAgent = class {
36029
36199
  approvalsReviewer: mode.approvalsReviewer,
36030
36200
  sandboxPolicy: mode.sandboxPolicy
36031
36201
  } : {},
36202
+ // The collaboration mode rides on the turn: Codex applies it to the
36203
+ // thread, where it persists until changed. "Build" is Codex's `default`
36204
+ // mode, so it is only sent once a choice exists to apply.
36205
+ ...this.#collaborationModeSupported && (selection.collaborationMode !== "default" || this.#collaborationModeSelected) ? {
36206
+ collaborationMode: {
36207
+ mode: selection.collaborationMode,
36208
+ settings: {
36209
+ developer_instructions: null,
36210
+ model: selection.model,
36211
+ reasoning_effort: selection.effort.length === 0 ? null : selection.effort
36212
+ }
36213
+ }
36214
+ } : {},
36032
36215
  ...selection.effort.length === 0 ? {} : { effort: selection.effort },
36033
36216
  input: input2,
36034
36217
  ...selection.model.length === 0 ? {} : { model: selection.model },
@@ -42818,6 +43001,70 @@ async function executeSessionPrepare(publisher, socket, command, config2, direct
42818
43001
  reportOperationFailure(socket, command.requestId, command.sessionId, error62, metrics.snapshot());
42819
43002
  }
42820
43003
  }
43004
+ function liveHistoryCursor(sessionId, endIndex) {
43005
+ return Buffer.from(JSON.stringify({ endIndex, sessionId }), "utf8").toString("base64url");
43006
+ }
43007
+ function liveHistoryEndIndex(cursor, sessionId) {
43008
+ try {
43009
+ if (!/^[A-Za-z0-9_-]{1,512}$/u.test(cursor)) {
43010
+ throw new Error("Invalid cursor alphabet");
43011
+ }
43012
+ const value = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
43013
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
43014
+ throw new Error("Invalid cursor shape");
43015
+ }
43016
+ const record2 = Object.fromEntries(Object.entries(value));
43017
+ if (Object.keys(record2).sort().join(",") !== "endIndex,sessionId" || record2["sessionId"] !== sessionId || typeof record2["endIndex"] !== "number" || !Number.isSafeInteger(record2["endIndex"]) || record2["endIndex"] < 0) {
43018
+ throw new Error("Invalid cursor target");
43019
+ }
43020
+ return record2["endIndex"];
43021
+ } catch (cause) {
43022
+ throw new AcpExecutionError("The native Session history cursor is invalid", { cause }, "invalid_cursor");
43023
+ }
43024
+ }
43025
+ function liveHistoryCoveredTurnRanges(history) {
43026
+ const ranges = [];
43027
+ let historyIndex = history.messages.length;
43028
+ for (const commandId of history.coveredCommandIds) {
43029
+ let assistantStartIndex = historyIndex;
43030
+ while (assistantStartIndex > 0 && history.messages[assistantStartIndex - 1]?.role === "assistant") {
43031
+ assistantStartIndex -= 1;
43032
+ }
43033
+ const userIndex = assistantStartIndex - 1;
43034
+ if (assistantStartIndex === historyIndex || history.messages[userIndex]?.role !== "user") {
43035
+ break;
43036
+ }
43037
+ ranges.push({ assistantEndIndex: historyIndex, assistantStartIndex, commandId, userIndex });
43038
+ historyIndex = userIndex;
43039
+ }
43040
+ return ranges;
43041
+ }
43042
+ function paginateLiveSessionHistory(history, sessionId, cursor, limit) {
43043
+ if (history.status === "unavailable") {
43044
+ if (cursor !== null) {
43045
+ throw new AcpExecutionError("Unavailable native Session history has no cursor", {}, "invalid_cursor");
43046
+ }
43047
+ return { cause: history.cause, nextCursor: null, status: "unavailable" };
43048
+ }
43049
+ const endIndex = cursor === null ? history.messages.length : liveHistoryEndIndex(cursor, sessionId);
43050
+ if (endIndex > history.messages.length) {
43051
+ throw new AcpExecutionError("The native Session history cursor is invalid", {}, "invalid_cursor");
43052
+ }
43053
+ const startIndex = Math.max(0, endIndex - limit);
43054
+ const coveredMessageIds = new Set(history.coveredMessageIds);
43055
+ const ranges = liveHistoryCoveredTurnRanges(history);
43056
+ return {
43057
+ coveredCommandIds: ranges.filter((range) => range.userIndex >= startIndex && range.assistantEndIndex <= endIndex).map((range) => range.commandId),
43058
+ coveredMessageIds: ranges.flatMap((range) => [
43059
+ ...range.userIndex >= startIndex && range.userIndex < endIndex && coveredMessageIds.has(`${range.commandId}:user`) ? [`${range.commandId}:user`] : [],
43060
+ ...range.assistantStartIndex >= startIndex && range.assistantEndIndex <= endIndex && coveredMessageIds.has(`${range.commandId}:agent`) ? [`${range.commandId}:agent`] : []
43061
+ ]),
43062
+ messages: history.messages.slice(startIndex, endIndex),
43063
+ nextCursor: startIndex > 0 ? liveHistoryCursor(sessionId, startIndex) : null,
43064
+ status: "available",
43065
+ truncated: history.truncated
43066
+ };
43067
+ }
42821
43068
  async function executeSessionLiveHistoryLoad(socket, command, config2, directories, audit, sessions, signal) {
42822
43069
  try {
42823
43070
  const resolved = await resolveSessionDirectories(
@@ -42867,6 +43114,57 @@ async function executeSessionLiveHistoryLoad(socket, command, config2, directori
42867
43114
  });
42868
43115
  }
42869
43116
  }
43117
+ async function executeSessionLiveHistoryPageLoad(socket, command, config2, directories, audit, sessions, signal) {
43118
+ try {
43119
+ const resolved = await resolveSessionDirectories(
43120
+ directories,
43121
+ config2.bindingId,
43122
+ command.directoryId,
43123
+ command.additionalDirectoryIds,
43124
+ signal
43125
+ );
43126
+ const history = await sessions.loadSessionHistory({
43127
+ additionalDirectories: resolved.additionalDirectories,
43128
+ agent: config2.catalog.find(command.agentId),
43129
+ cwd: resolved.cwd,
43130
+ sessionId: command.sessionId,
43131
+ signal
43132
+ });
43133
+ signal.throwIfAborted();
43134
+ const page = paginateLiveSessionHistory(history, command.sessionId, command.cursor, command.limit);
43135
+ audit.record({
43136
+ commandId: command.requestId,
43137
+ event: "session_history_loaded",
43138
+ sessionId: command.sessionId
43139
+ });
43140
+ send(socket, {
43141
+ kind: "session.live-history.page.loaded",
43142
+ page,
43143
+ requestId: command.requestId,
43144
+ sessionId: command.sessionId
43145
+ });
43146
+ } catch (error62) {
43147
+ if (signal.aborted) {
43148
+ return;
43149
+ }
43150
+ audit.record({
43151
+ commandId: command.requestId,
43152
+ details: { code: operationErrorCode(error62) },
43153
+ event: "session_history_unavailable",
43154
+ sessionId: command.sessionId
43155
+ });
43156
+ send(socket, {
43157
+ kind: "session.live-history.page.loaded",
43158
+ page: {
43159
+ status: "unavailable",
43160
+ cause: { code: operationErrorCode(error62), message: operationErrorMessage(error62) },
43161
+ nextCursor: null
43162
+ },
43163
+ requestId: command.requestId,
43164
+ sessionId: command.sessionId
43165
+ });
43166
+ }
43167
+ }
42870
43168
  async function executeSessionConfigure(publisher, socket, command, config2, directories, audit, sessions, signal) {
42871
43169
  try {
42872
43170
  const resolved = await resolveSessionDirectories(
@@ -43726,6 +44024,19 @@ async function runRelayConnection(config2, directories, audit, sessions, install
43726
44024
  );
43727
44025
  return;
43728
44026
  }
44027
+ if (kind === SESSION_LIVE_HISTORY_PAGE_LOAD_KIND) {
44028
+ const command2 = decodeSessionLiveHistoryPageLoad(encoded);
44029
+ void executeSessionLiveHistoryPageLoad(
44030
+ socket,
44031
+ command2,
44032
+ config2,
44033
+ directories,
44034
+ audit,
44035
+ sessions,
44036
+ connectionController.signal
44037
+ );
44038
+ return;
44039
+ }
43729
44040
  if (kind === NATIVE_SESSION_CANCEL_KIND) {
43730
44041
  const command2 = message;
43731
44042
  const pending = nativeRequests.get(command2.requestId);