@karlorz/codexhost-cli-linux-x64 0.22.0-5 → 0.22.0-7

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/README.md CHANGED
@@ -7,7 +7,7 @@ Run Pi and Claude Code as first-class external harnesses inside Codex Desktop.
7
7
  ## Install
8
8
 
9
9
  ```bash
10
- npm install -g @karlorz/codexhost-cli@0.22.0-5
10
+ npm install -g @karlorz/codexhost-cli@0.22.0-7
11
11
  ```
12
12
 
13
13
  Do not install this package directly. npm selects it through the optional dependencies of `@karlorz/codexhost-cli`.
@@ -1 +1 @@
1
- {"schemaVersion":1,"version":"0.22.0-5","distribution":"npm","target":"linux-x64"}
1
+ {"schemaVersion":1,"version":"0.22.0-7","distribution":"npm","target":"linux-x64"}
@@ -18242,7 +18242,7 @@ var SessionImportScope = class {
18242
18242
  };
18243
18243
 
18244
18244
  // dist/native-history.js
18245
- import { existsSync as existsSync2, readFileSync, realpathSync } from "node:fs";
18245
+ import { existsSync as existsSync2, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
18246
18246
  import os2 from "node:os";
18247
18247
  import path6 from "node:path";
18248
18248
  import { DatabaseSync } from "node:sqlite";
@@ -18265,6 +18265,13 @@ function cursorSessionDirectory(sessionId, environment) {
18265
18265
  throw new Error("Unsupported Cursor native session ID");
18266
18266
  return path6.join(cursorConfigDirectory(environment), "acp-sessions", sessionId);
18267
18267
  }
18268
+ function cursorNativeStoreExists(sessionId, environment) {
18269
+ try {
18270
+ return existsSync2(path6.join(cursorSessionDirectory(sessionId, environment), "store.db"));
18271
+ } catch {
18272
+ return false;
18273
+ }
18274
+ }
18268
18275
  function cursorSameWorkspace(left, right) {
18269
18276
  return path6.resolve(left) === path6.resolve(right) || existsSync2(left) && existsSync2(right) && realpathSync.native(left) === realpathSync.native(right);
18270
18277
  }
@@ -18366,6 +18373,47 @@ function readCursorNativeHistory(sessionId, cwd, environment, allowMissing = fal
18366
18373
  db.close();
18367
18374
  }
18368
18375
  }
18376
+ function acpOnlyHistoryFile(sessionId, environment) {
18377
+ return path6.join(cursorConfigDirectory(environment), "codexhost-acp-only", `${sessionId}.json`);
18378
+ }
18379
+ function readCursorAcpOnlyTurns(sessionId, environment) {
18380
+ try {
18381
+ cursorSessionDirectory(sessionId, environment);
18382
+ const filename = acpOnlyHistoryFile(sessionId, environment);
18383
+ if (!existsSync2(filename))
18384
+ return [];
18385
+ const raw = JSON.parse(readFileSync(filename, "utf8"));
18386
+ if (!raw || typeof raw !== "object" || !Array.isArray(raw.turns))
18387
+ return [];
18388
+ const turns = [];
18389
+ for (const item of raw.turns) {
18390
+ if (!item || typeof item !== "object")
18391
+ continue;
18392
+ const id = item.id;
18393
+ const text = item.text;
18394
+ if (typeof id !== "string" || !id.trim() || typeof text !== "string")
18395
+ continue;
18396
+ const assistantText = item.assistantText;
18397
+ const itemId = item.itemId;
18398
+ turns.push({
18399
+ id,
18400
+ text,
18401
+ ...typeof assistantText === "string" && assistantText.trim() ? { assistantText } : {},
18402
+ ...typeof itemId === "string" && itemId.trim() ? { itemId } : {}
18403
+ });
18404
+ }
18405
+ return turns;
18406
+ } catch {
18407
+ return [];
18408
+ }
18409
+ }
18410
+ function writeCursorAcpOnlyTurns(sessionId, environment, turns) {
18411
+ cursorSessionDirectory(sessionId, environment);
18412
+ const filename = acpOnlyHistoryFile(sessionId, environment);
18413
+ mkdirSync(path6.dirname(filename), { recursive: true });
18414
+ writeFileSync(filename, `${JSON.stringify({ version: 1, nativeSessionId: sessionId, turns })}
18415
+ `, "utf8");
18416
+ }
18369
18417
  function readCursorNativeTurns(sessionId, cwd, environment, allowMissing = false) {
18370
18418
  return readCursorNativeHistory(sessionId, cwd, environment, allowMissing).turns;
18371
18419
  }
@@ -18383,6 +18431,14 @@ function cursorNativeSessionRef(nativeSessionId, executionPolicy) {
18383
18431
  formatVersion: 1
18384
18432
  });
18385
18433
  }
18434
+ function cursorNativeTurnRef(nativeSessionId, nativeTurnKey) {
18435
+ return nativeTurnRefSchema.parse({
18436
+ harnessId: cursorHarnessId,
18437
+ nativeSessionId,
18438
+ nativeTurnKey,
18439
+ formatVersion: 1
18440
+ });
18441
+ }
18386
18442
  function cursorExecutionPolicy(ref) {
18387
18443
  if (ref.harnessId !== cursorHarnessId || ref.formatVersion !== 1)
18388
18444
  throw new Error("Invalid Cursor Native Session Ref");
@@ -18619,6 +18675,83 @@ var CURSOR_MODES = harnessPermissionModeCatalogSchema.parse({
18619
18675
  }
18620
18676
  ]
18621
18677
  });
18678
+ var GROK_ACP_MODES = harnessPermissionModeCatalogSchema.parse({
18679
+ defaultModeId: "default",
18680
+ modes: [
18681
+ {
18682
+ id: "default",
18683
+ label: "Default",
18684
+ description: "Native ACP default session mode"
18685
+ },
18686
+ {
18687
+ id: "plan",
18688
+ label: "Plan",
18689
+ description: "Native ACP plan session mode"
18690
+ }
18691
+ ]
18692
+ });
18693
+ var HOST_RUN_EVERYTHING_MODE_ID = harnessPermissionModeIdSchema.parse("always-approve");
18694
+ var HOST_RUN_EVERYTHING_MODE = harnessPermissionModeSchema.parse({
18695
+ id: HOST_RUN_EVERYTHING_MODE_ID,
18696
+ label: "Run everything",
18697
+ description: "Auto-allow native tool approvals in this Host session. Does not change ACP session mode.",
18698
+ dangerous: true
18699
+ });
18700
+ function parsedPermissionModeId(value) {
18701
+ const parsed = harnessPermissionModeIdSchema.safeParse(value);
18702
+ return parsed.success ? parsed.data : void 0;
18703
+ }
18704
+ function cursorUsesNativeSessionModes(info) {
18705
+ return cursorModes(info) !== CURSOR_MODES;
18706
+ }
18707
+ function cursorNativeModeIsWritable(modeId) {
18708
+ return modeId !== "plan" && modeId !== "ask";
18709
+ }
18710
+ function cursorRunnableNativeModeId(info) {
18711
+ const native = cursorModes(info);
18712
+ if (cursorUsesNativeSessionModes(info) && native.modes.some((mode) => mode.id === "default"))
18713
+ return "default";
18714
+ if (native.modes.some((mode) => mode.id === "agent"))
18715
+ return "agent";
18716
+ return native.modes.find((mode) => cursorNativeModeIsWritable(mode.id))?.id ?? native.defaultModeId;
18717
+ }
18718
+ function cursorHostPermissionModes(info, options = {}) {
18719
+ const native = cursorModes(info);
18720
+ const nativeOwnsHostId = native.modes.some((mode) => mode.id === HOST_RUN_EVERYTHING_MODE_ID);
18721
+ const modes = nativeOwnsHostId ? native.modes : [...native.modes, HOST_RUN_EVERYTHING_MODE];
18722
+ const nativeCurrent = cursorCurrentPermissionModeId(info);
18723
+ const writable = cursorNativeModeIsWritable(nativeCurrent);
18724
+ const preferred = options.effectiveHostModeId;
18725
+ const defaultModeId = preferred && modes.some((mode) => mode.id === preferred) && !(preferred === HOST_RUN_EVERYTHING_MODE_ID && !writable) ? preferred : !nativeOwnsHostId && options.unrestricted && writable ? HOST_RUN_EVERYTHING_MODE_ID : native.defaultModeId;
18726
+ return harnessPermissionModeCatalogSchema.parse({ modes, defaultModeId });
18727
+ }
18728
+ function cursorModes(info) {
18729
+ const available = (info.modes?.availableModes ?? []).flatMap((mode) => {
18730
+ const id = parsedPermissionModeId(mode.id);
18731
+ return id ? [
18732
+ {
18733
+ id,
18734
+ label: mode.name,
18735
+ ...mode.description ? { description: mode.description } : {}
18736
+ }
18737
+ ] : [];
18738
+ });
18739
+ const currentMode = info.modes?.currentModeId;
18740
+ if (available.length) {
18741
+ const current = available.some((mode) => mode.id === currentMode) ? currentMode : available[0]?.id;
18742
+ if (current)
18743
+ return harnessPermissionModeCatalogSchema.parse({ modes: available, defaultModeId: current });
18744
+ }
18745
+ if (GROK_ACP_MODES.modes.some((mode) => mode.id === currentMode) && currentMode)
18746
+ return harnessPermissionModeCatalogSchema.parse({
18747
+ ...GROK_ACP_MODES,
18748
+ defaultModeId: currentMode
18749
+ });
18750
+ return CURSOR_MODES;
18751
+ }
18752
+ function cursorCurrentPermissionModeId(info) {
18753
+ return harnessPermissionModeIdSchema.parse(info.configOptions?.find((option) => option.id === "mode")?.currentValue ?? info.modes?.currentModeId ?? CURSOR_MODES.defaultModeId);
18754
+ }
18622
18755
  var cursorModelRef = (nativeId) => harnessModelRefSchema.parse({ id: `cursor.${Buffer.from(nativeId).toString("base64url")}` });
18623
18756
  function cursorModels(info) {
18624
18757
  const option = info.configOptions?.find((option2) => option2.id === "model");
@@ -22940,7 +23073,7 @@ var CursorTransport = class {
22940
23073
  return this.#preparation ??= this.#prepare();
22941
23074
  }
22942
23075
  async #prepare() {
22943
- const useForce = this.options.executionPolicy === "unattended-full-access" || cursorCliApprovalModeIsUnrestricted(this.options.environment, this.options.cwd);
23076
+ const useForce = this.options.executionPolicy === "unattended-full-access" || this.options.hostRunEverything === true || cursorCliApprovalModeIsUnrestricted(this.options.environment, this.options.cwd);
22944
23077
  const invocation = cursorInvocation(this.options.environment, this.options.command, useForce ? ["--force", "acp"] : ["acp"]);
22945
23078
  const child = spawn(invocation.command, invocation.arguments, {
22946
23079
  cwd: this.options.cwd,
@@ -23036,6 +23169,11 @@ var CursorTransport = class {
23036
23169
  throw new Error("Cursor session is not open");
23037
23170
  return this.#bounded(this.#connection.setSessionConfigOption({ sessionId: this.sessionId, configId, value }));
23038
23171
  }
23172
+ async setMode(modeId) {
23173
+ if (!this.#connection)
23174
+ throw new Error("Cursor session is not open");
23175
+ return this.#bounded(this.#connection.setSessionMode({ sessionId: this.sessionId, modeId }));
23176
+ }
23039
23177
  async prompt(text, callbacks) {
23040
23178
  if (!this.#connection || this.#closed || this.#callbacks)
23041
23179
  throw new Error("Cursor session is closed or busy");
@@ -23808,6 +23946,7 @@ var CursorTurnOutput = class {
23808
23946
  #hold = "";
23809
23947
  #writableIterableClosed = false;
23810
23948
  #visibleAssistantText = false;
23949
+ #agentMessage;
23811
23950
  constructor(turnId, emit, nativeTurnIndex = 0) {
23812
23951
  this.turnId = turnId;
23813
23952
  this.emit = emit;
@@ -23825,6 +23964,13 @@ var CursorTurnOutput = class {
23825
23964
  hasVisibleAssistantText() {
23826
23965
  return this.#visibleAssistantText || takeCursorWritableIterableClosed(this.#hold).visible.trim().length > 0;
23827
23966
  }
23967
+ visibleAgentMessage() {
23968
+ if (this.#agentMessage)
23969
+ return this.#agentMessage;
23970
+ if (this.#text?.type === "agentMessage" && this.#text.text.trim())
23971
+ return { itemId: this.#text.itemId, text: this.#text.text };
23972
+ return void 0;
23973
+ }
23828
23974
  #flushHold() {
23829
23975
  if (!this.#hold)
23830
23976
  return;
@@ -23858,6 +24004,8 @@ var CursorTurnOutput = class {
23858
24004
  });
23859
24005
  }
23860
24006
  #finishText(outcome = { status: "succeeded" }) {
24007
+ if (this.#text?.type === "agentMessage" && this.#text.text.trim())
24008
+ this.#agentMessage = { itemId: this.#text.itemId, text: this.#text.text };
23861
24009
  if (this.#text)
23862
24010
  this.emit({
23863
24011
  type: "item.completed",
@@ -24038,10 +24186,14 @@ import { randomUUID } from "node:crypto";
24038
24186
  var CursorInteractions = class {
24039
24187
  emit;
24040
24188
  refuseUnattendedApproval;
24189
+ autoAllowNativeToolApprovals;
24190
+ refuseMissingAllowOption;
24041
24191
  #pending = /* @__PURE__ */ new Map();
24042
- constructor(emit, refuseUnattendedApproval) {
24192
+ constructor(emit, refuseUnattendedApproval, autoAllowNativeToolApprovals, refuseMissingAllowOption) {
24043
24193
  this.emit = emit;
24044
24194
  this.refuseUnattendedApproval = refuseUnattendedApproval;
24195
+ this.autoAllowNativeToolApprovals = autoAllowNativeToolApprovals;
24196
+ this.refuseMissingAllowOption = refuseMissingAllowOption;
24045
24197
  }
24046
24198
  #ask(interaction) {
24047
24199
  return new Promise((resolve) => {
@@ -24054,6 +24206,14 @@ var CursorInteractions = class {
24054
24206
  await this.refuseUnattendedApproval();
24055
24207
  return { outcome: { outcome: "cancelled" } };
24056
24208
  }
24209
+ if (this.autoAllowNativeToolApprovals?.()) {
24210
+ const selected = request.options.find((option) => option.kind === "allow_always") ?? request.options.find((option) => option.kind === "allow_once");
24211
+ if (!selected) {
24212
+ await this.refuseMissingAllowOption?.();
24213
+ return { outcome: { outcome: "cancelled" } };
24214
+ }
24215
+ return { outcome: { outcome: "selected", optionId: selected.optionId } };
24216
+ }
24057
24217
  const response = await this.#ask({
24058
24218
  type: "approval",
24059
24219
  interactionId: hostInteractionIdSchema.parse(randomUUID()),
@@ -24527,7 +24687,33 @@ var CursorAdapter = class {
24527
24687
  #sessions = /* @__PURE__ */ new Set();
24528
24688
  #forks = /* @__PURE__ */ new Map();
24529
24689
  #inspections = /* @__PURE__ */ new Map();
24690
+ /** Grok 2.0.7 holds GROK_HOME for one stdio child. Inspect and open must not overlap. */
24691
+ #stdioBusy = false;
24692
+ #liveStdioSession;
24530
24693
  #closed = false;
24694
+ #tryAcquireStdio() {
24695
+ if (this.#stdioBusy)
24696
+ return false;
24697
+ this.#stdioBusy = true;
24698
+ return true;
24699
+ }
24700
+ #releaseStdio() {
24701
+ this.#stdioBusy = false;
24702
+ }
24703
+ #stdioBusyError() {
24704
+ const id = this.#liveStdioSession?.initialState.nativeRef?.nativeSessionId;
24705
+ return rejected("sessionBusy", id ? `Grok ACP is busy with session ${id}; close that cursor-cli thread or continue there` : "Grok ACP is already starting a session; close that cursor-cli thread or retry");
24706
+ }
24707
+ #busyInspection() {
24708
+ return {
24709
+ status: "unavailable",
24710
+ error: {
24711
+ code: "unavailable",
24712
+ message: "Grok ACP is busy; retry inspect after the live session is ready",
24713
+ retryable: true
24714
+ }
24715
+ };
24716
+ }
24531
24717
  sessionImport;
24532
24718
  constructor(options = {}) {
24533
24719
  this.options = options;
@@ -24546,6 +24732,21 @@ var CursorAdapter = class {
24546
24732
  ...this.options.timeoutMs ? { timeoutMs: this.options.timeoutMs } : {}
24547
24733
  };
24548
24734
  }
24735
+ #inspectionFromLiveSession(cwd) {
24736
+ const sessions = [...this.#sessions];
24737
+ const match = sessions.find((session) => path9.resolve(session.transport.options.cwd) === cwd) ?? sessions[0];
24738
+ if (!match)
24739
+ return;
24740
+ return {
24741
+ status: "ready",
24742
+ catalog: cursorCatalog(match.info),
24743
+ capabilities: cursorCapabilities(match.info),
24744
+ permissionModes: cursorHostPermissionModes(match.info, {
24745
+ ...match.initialState.effectivePermissionModeId ? { effectiveHostModeId: match.initialState.effectivePermissionModeId } : {},
24746
+ unrestricted: cursorCliApprovalModeIsUnrestricted(match.transport.options.environment, match.transport.options.cwd)
24747
+ })
24748
+ };
24749
+ }
24549
24750
  async inspect(input = {}) {
24550
24751
  if (this.#closed)
24551
24752
  return {
@@ -24553,27 +24754,42 @@ var CursorAdapter = class {
24553
24754
  error: { code: "unavailable", message: "Cursor adapter is closed", retryable: false }
24554
24755
  };
24555
24756
  const cwd = path9.resolve(input.cwd ?? process.cwd());
24757
+ const live = this.#inspectionFromLiveSession(cwd);
24758
+ if (live)
24759
+ return live;
24760
+ if (this.#stdioBusy)
24761
+ return this.#busyInspection();
24556
24762
  const cached2 = this.#inspections.get(cwd);
24557
24763
  if (cached2 && (cached2.pending || !input.refresh))
24558
24764
  return cached2.result;
24559
24765
  const result = (async () => {
24560
- const transport = new CursorTransport(this.transportOptions(cwd));
24766
+ if (!this.#tryAcquireStdio()) {
24767
+ this.#inspections.delete(cwd);
24768
+ return this.#busyInspection();
24769
+ }
24561
24770
  try {
24562
- const info = await transport.open();
24563
- return {
24564
- status: "ready",
24565
- catalog: cursorCatalog(info),
24566
- capabilities: cursorCapabilities(info),
24567
- permissionModes: CURSOR_MODES
24568
- };
24569
- } catch (error51) {
24570
- const failure = cursorError(error51);
24571
- return {
24572
- status: failure.code === "notInstalled" ? "notInstalled" : "unavailable",
24573
- error: failure
24574
- };
24771
+ const transport = new CursorTransport(this.transportOptions(cwd));
24772
+ try {
24773
+ const info = await transport.open();
24774
+ return {
24775
+ status: "ready",
24776
+ catalog: cursorCatalog(info),
24777
+ capabilities: cursorCapabilities(info),
24778
+ permissionModes: cursorHostPermissionModes(info, {
24779
+ unrestricted: cursorCliApprovalModeIsUnrestricted(transport.options.environment, transport.options.cwd)
24780
+ })
24781
+ };
24782
+ } catch (error51) {
24783
+ const failure = cursorError(error51);
24784
+ return {
24785
+ status: failure.code === "notInstalled" ? "notInstalled" : "unavailable",
24786
+ error: failure
24787
+ };
24788
+ } finally {
24789
+ await transport.close();
24790
+ }
24575
24791
  } finally {
24576
- await transport.close();
24792
+ this.#releaseStdio();
24577
24793
  }
24578
24794
  })();
24579
24795
  const entry = { pending: true, result };
@@ -24619,55 +24835,92 @@ var CursorAdapter = class {
24619
24835
  }
24620
24836
  const options = {
24621
24837
  ...this.transportOptions(input.cwd, input.environment),
24622
- executionPolicy
24838
+ executionPolicy,
24839
+ ...input.permissionModeId === HOST_RUN_EVERYTHING_MODE_ID ? { hostRunEverything: true } : {}
24623
24840
  };
24624
24841
  const transport = prepared ?? new CursorTransport(options);
24625
- try {
24626
- const before = input.kind === "resume" ? readCursorNativeHistory(input.nativeRef.nativeSessionId, options.cwd, options.environment) : void 0;
24627
- const info = await transport.open(input.kind === "resume" ? input.nativeRef.nativeSessionId : void 0);
24628
- let loadedSnapshot;
24629
- if (input.kind === "resume") {
24630
- const native = readCursorNativeHistory(transport.sessionId, options.cwd, options.environment);
24631
- const snapshot = cursorSnapshot(transport.sessionId, native.turns, transport.replay);
24632
- if (input.knownTurnRefs?.some((ref) => ref.harnessId !== this.harnessId || ref.nativeSessionId !== transport.sessionId || !native.turns.some((turn) => turn.id === ref.nativeTurnKey)))
24633
- throw new Error("Saved Cursor turn identity no longer exists in native history");
24634
- if (native.revision && before?.revision === native.revision)
24635
- loadedSnapshot = { revision: native.revision, snapshot };
24636
- }
24637
- const session = new CursorSession(transport, info, () => {
24638
- this.#sessions.delete(session);
24639
- }, input.kind === "create", loadedSnapshot);
24640
- if (input.model) {
24641
- const selected = await session.execute({ type: "model.select", model: input.model });
24642
- if (!selected.ok)
24643
- throw new Error(selected.error.message);
24644
- }
24645
- if (input.thinkingOptionId) {
24646
- const selected = await session.execute({
24647
- type: "thinking.select",
24648
- thinkingOptionId: input.thinkingOptionId
24649
- });
24650
- if (!selected.ok)
24651
- throw new Error(selected.error.message);
24652
- }
24653
- if (input.permissionModeId) {
24654
- const selected = await session.execute({
24655
- type: "permissionMode.select",
24656
- permissionModeId: input.permissionModeId
24657
- });
24658
- if (!selected.ok)
24659
- throw new Error(selected.error.message);
24842
+ let session;
24843
+ const held = !prepared;
24844
+ const run = async () => {
24845
+ let acquired = false;
24846
+ if (held) {
24847
+ const live = this.#liveStdioSession;
24848
+ if (live) {
24849
+ const liveId = live.initialState.nativeRef?.nativeSessionId;
24850
+ if (input.kind === "resume" && liveId && input.nativeRef.nativeSessionId === liveId)
24851
+ return { ok: true, value: live };
24852
+ if (!live.stdioIdle())
24853
+ return this.#stdioBusyError();
24854
+ await live.close();
24855
+ }
24856
+ if (!this.#tryAcquireStdio())
24857
+ return this.#stdioBusyError();
24858
+ acquired = true;
24660
24859
  }
24661
- if (this.#closed) {
24662
- await session.close();
24663
- return rejected("invalidState", "Cursor adapter closed during session startup");
24860
+ try {
24861
+ const acpOnlyResume = input.kind === "resume" && !cursorNativeStoreExists(input.nativeRef.nativeSessionId, options.environment);
24862
+ const before = input.kind === "resume" && !acpOnlyResume ? readCursorNativeHistory(input.nativeRef.nativeSessionId, options.cwd, options.environment) : void 0;
24863
+ let info;
24864
+ if (acpOnlyResume) {
24865
+ info = await transport.open();
24866
+ } else {
24867
+ info = await transport.open(input.kind === "resume" ? input.nativeRef.nativeSessionId : void 0);
24868
+ }
24869
+ let loadedSnapshot;
24870
+ if (input.kind === "resume" && !acpOnlyResume) {
24871
+ const native = readCursorNativeHistory(transport.sessionId, options.cwd, options.environment);
24872
+ const snapshot = cursorSnapshot(transport.sessionId, native.turns, transport.replay);
24873
+ if (input.knownTurnRefs?.some((ref) => ref.harnessId !== this.harnessId || ref.nativeSessionId !== transport.sessionId || !native.turns.some((turn) => turn.id === ref.nativeTurnKey)))
24874
+ throw new Error("Saved Cursor turn identity no longer exists in native history");
24875
+ if (native.revision && before?.revision === native.revision)
24876
+ loadedSnapshot = { revision: native.revision, snapshot };
24877
+ }
24878
+ session = new CursorSession(transport, info, () => {
24879
+ const opened = session;
24880
+ if (!opened)
24881
+ return;
24882
+ this.#sessions.delete(opened);
24883
+ if (this.#liveStdioSession === opened) {
24884
+ this.#liveStdioSession = void 0;
24885
+ this.#releaseStdio();
24886
+ }
24887
+ }, input.kind === "create" || acpOnlyResume, loadedSnapshot, input.kind === "resume" ? input.nativeRef.nativeSessionId : void 0);
24888
+ this.#sessions.add(session);
24889
+ const restoreOrThrow = async (selected) => {
24890
+ if (!selected.ok && !acpOnlyResume)
24891
+ throw new Error(selected.error.message);
24892
+ };
24893
+ if (input.model)
24894
+ await restoreOrThrow(await session.execute({ type: "model.select", model: input.model }));
24895
+ if (input.thinkingOptionId)
24896
+ await restoreOrThrow(await session.execute({
24897
+ type: "thinking.select",
24898
+ thinkingOptionId: input.thinkingOptionId
24899
+ }));
24900
+ if (input.permissionModeId && cursorHostPermissionModes(info).modes.some((mode) => mode.id === input.permissionModeId))
24901
+ await restoreOrThrow(await session.execute({
24902
+ type: "permissionMode.select",
24903
+ permissionModeId: input.permissionModeId
24904
+ }));
24905
+ if (this.#closed) {
24906
+ await session.close();
24907
+ return rejected("invalidState", "Cursor adapter closed during session startup");
24908
+ }
24909
+ const acpOnly = acpOnlyResume || !cursorNativeStoreExists(transport.sessionId, options.environment);
24910
+ if (held && acpOnly)
24911
+ this.#liveStdioSession = session;
24912
+ return { ok: true, value: session };
24913
+ } catch (error51) {
24914
+ await session?.close();
24915
+ if (!session)
24916
+ await transport.close();
24917
+ return { ok: false, error: cursorError(error51) };
24918
+ } finally {
24919
+ if (acquired && !this.#liveStdioSession)
24920
+ this.#releaseStdio();
24664
24921
  }
24665
- this.#sessions.add(session);
24666
- return { ok: true, value: session };
24667
- } catch (error51) {
24668
- await transport.close();
24669
- return { ok: false, error: cursorError(error51) };
24670
- }
24922
+ };
24923
+ return run();
24671
24924
  }
24672
24925
  async #fork(input, controller) {
24673
24926
  const signal = controller.signal;
@@ -24713,7 +24966,12 @@ var CursorAdapter = class {
24713
24966
  const sameEnvironment = sourceEnvironment && [
24714
24967
  .../* @__PURE__ */ new Set([...Object.keys(sourceEnvironment), ...Object.keys(options.environment)])
24715
24968
  ].every((key) => sourceEnvironment[key] === options.environment[key]);
24716
- transport = new CursorTransport({ ...options, executionPolicy }, sameEnvironment ? source?.info.nativeModels : void 0);
24969
+ const mode = input.kind === "rollbackLastTurn" ? input.permissionModeId ?? source?.initialState.effectivePermissionModeId : source?.initialState.effectivePermissionModeId;
24970
+ transport = new CursorTransport({
24971
+ ...options,
24972
+ executionPolicy,
24973
+ hostRunEverything: mode === HOST_RUN_EVERYTHING_MODE_ID
24974
+ }, sameEnvironment ? source?.info.nativeModels : void 0);
24717
24975
  const abort = (error51) => {
24718
24976
  controller.abort(error51);
24719
24977
  throw error51;
@@ -24733,7 +24991,6 @@ var CursorAdapter = class {
24733
24991
  throw new Error("Cursor native fork did not return a target");
24734
24992
  const model2 = input.kind === "rollbackLastTurn" ? input.model ?? source?.initialState.effectiveModel : source?.initialState.effectiveModel;
24735
24993
  const thinking = input.kind === "rollbackLastTurn" ? input.thinkingOptionId ?? source?.initialState.effectiveThinkingOptionId : source?.initialState.effectiveThinkingOptionId;
24736
- const mode = input.kind === "rollbackLastTurn" ? input.permissionModeId ?? source?.initialState.effectivePermissionModeId : source?.initialState.effectivePermissionModeId;
24737
24994
  const opened = await this.#openSession({
24738
24995
  kind: "resume",
24739
24996
  cwd: input.cwd,
@@ -24770,6 +25027,8 @@ var CursorAdapter = class {
24770
25027
  controller.abort();
24771
25028
  await Promise.allSettled([importing, ...this.#forks.values()]);
24772
25029
  await Promise.allSettled([...this.#sessions].map((session) => session.close()));
25030
+ this.#liveStdioSession = void 0;
25031
+ this.#stdioBusy = false;
24773
25032
  await Promise.allSettled([...this.#inspections.values()].map((inspection) => inspection.result));
24774
25033
  }
24775
25034
  };
@@ -24805,15 +25064,22 @@ var CursorSession = class {
24805
25064
  #submitted = /* @__PURE__ */ new Set();
24806
25065
  #active;
24807
25066
  #unattendedApproval = false;
25067
+ #missingAllowOption = false;
25068
+ #autoAllowNativeToolApprovals = false;
24808
25069
  #configuring = false;
24809
25070
  #closed = false;
24810
25071
  #fresh;
25072
+ #nativeSessionId;
25073
+ #acpOnlyTurns;
24811
25074
  #snapshot;
24812
25075
  #subagentOutput;
24813
25076
  /** A Turn is running and no configuration change is in flight. */
24814
25077
  turnRunningForFork() {
24815
25078
  return !this.#closed && !this.#configuring && this.#active !== void 0;
24816
25079
  }
25080
+ stdioIdle() {
25081
+ return this.#closed || !this.#active && !this.#configuring;
25082
+ }
24817
25083
  lockForFork() {
24818
25084
  if (this.#closed || this.#active || this.#configuring)
24819
25085
  return;
@@ -24829,30 +25095,111 @@ var CursorSession = class {
24829
25095
  return void 0;
24830
25096
  }
24831
25097
  }
24832
- constructor(transport, info, onClose, created = true, loadedSnapshot) {
25098
+ constructor(transport, info, onClose, created = true, loadedSnapshot, persistedSessionId) {
24833
25099
  this.transport = transport;
24834
25100
  this.info = info;
24835
25101
  this.onClose = onClose;
24836
25102
  this.info = structuredClone(info);
24837
25103
  this.#fresh = created;
25104
+ this.#nativeSessionId = persistedSessionId ?? transport.sessionId;
25105
+ this.#acpOnlyTurns = readCursorAcpOnlyTurns(this.#nativeSessionId, transport.options.environment);
24838
25106
  this.#snapshot = loadedSnapshot;
24839
25107
  this.#executionPolicy = transport.options.executionPolicy ?? "default";
24840
- this.#interactions = new CursorInteractions((output) => this.#channel.emit(output), this.#executionPolicy === "unattended-full-access" ? () => this.#refuseUnattendedApproval() : void 0);
25108
+ const nativeCurrent = cursorCurrentPermissionModeId(info);
25109
+ const unrestricted = cursorCliApprovalModeIsUnrestricted(transport.options.environment, transport.options.cwd);
25110
+ this.#autoAllowNativeToolApprovals = this.#executionPolicy !== "unattended-full-access" && cursorNativeModeIsWritable(nativeCurrent) && (transport.options.hostRunEverything === true || unrestricted);
25111
+ this.#interactions = new CursorInteractions((output) => this.#channel.emit(output), this.#executionPolicy === "unattended-full-access" ? () => this.#refuseUnattendedApproval() : void 0, () => this.#autoAllowNativeToolApprovals, () => this.#refuseMissingAllowOption());
24841
25112
  this.initialState = {
24842
- nativeRef: cursorNativeSessionRef(transport.sessionId, this.#executionPolicy),
25113
+ nativeRef: cursorNativeSessionRef(this.#nativeSessionId, this.#executionPolicy),
24843
25114
  effectiveModel: cursorConfiguredModelRef(cursorModels(info).current, info.configOptions),
24844
25115
  ...cursorThinkingState(info),
24845
- effectivePermissionModeId: harnessPermissionModeIdSchema.parse(info.configOptions?.find((option) => option.id === "mode")?.currentValue ?? info.modes?.currentModeId ?? "agent")
25116
+ effectivePermissionModeId: this.#autoAllowNativeToolApprovals ? HOST_RUN_EVERYTHING_MODE_ID : nativeCurrent
24846
25117
  };
24847
25118
  }
25119
+ #emitState() {
25120
+ this.#channel.emit({
25121
+ kind: "event",
25122
+ event: { type: "session.state.changed", state: structuredClone(this.initialState) }
25123
+ });
25124
+ }
25125
+ async #applyNativeMode(modeId) {
25126
+ if (cursorCurrentPermissionModeId(this.info) === modeId)
25127
+ return { ok: true, value: { completed: true } };
25128
+ if (!cursorModes(this.info).modes.some((mode) => mode.id === modeId))
25129
+ return rejected("invalidRequest", "Unknown Cursor execution mode");
25130
+ try {
25131
+ if (cursorUsesNativeSessionModes(this.info)) {
25132
+ await this.transport.setMode(modeId);
25133
+ this.info.modes = {
25134
+ currentModeId: modeId,
25135
+ availableModes: this.info.modes?.availableModes ?? []
25136
+ };
25137
+ return { ok: true, value: { completed: true } };
25138
+ }
25139
+ const result = await this.transport.configure("mode", modeId);
25140
+ const confirmed = result.configOptions.find((option) => option.id === "mode" && option.currentValue === modeId);
25141
+ if (!confirmed)
25142
+ throw new Error("Cursor did not confirm configuration selection");
25143
+ this.info.configOptions = result.configOptions.some((option) => option.id === "model") ? result.configOptions : [...(this.info.configOptions ?? []).filter((option) => option.id !== "mode"), confirmed];
25144
+ return { ok: true, value: { completed: true } };
25145
+ } catch (error51) {
25146
+ return { ok: false, error: cursorError(error51) };
25147
+ }
25148
+ }
24848
25149
  #native(allowMissing = false) {
24849
- return readCursorNativeTurns(this.transport.sessionId, this.transport.options.cwd, this.transport.options.environment, allowMissing);
25150
+ const missing = !cursorNativeStoreExists(this.transport.sessionId, this.transport.options.environment);
25151
+ try {
25152
+ return readCursorNativeTurns(this.transport.sessionId, this.transport.options.cwd, this.transport.options.environment, allowMissing || missing);
25153
+ } catch (error51) {
25154
+ if (allowMissing || missing)
25155
+ return [];
25156
+ throw error51;
25157
+ }
25158
+ }
25159
+ #acpOnlyNativeStore() {
25160
+ return !cursorNativeStoreExists(this.transport.sessionId, this.transport.options.environment);
25161
+ }
25162
+ #acpOnlySnapshotTurns() {
25163
+ return this.#acpOnlyTurns.map((turn) => ({
25164
+ nativeTurnRef: cursorNativeTurnRef(this.#nativeSessionId, turn.id),
25165
+ input: [{ type: "text", text: turn.text }],
25166
+ items: turn.assistantText ? [
25167
+ {
25168
+ item: {
25169
+ type: "agentMessage",
25170
+ itemId: hostItemIdSchema.parse(turn.itemId ?? `cursor-${turn.id}-1`),
25171
+ text: turn.assistantText
25172
+ },
25173
+ outcome: { status: "succeeded" }
25174
+ }
25175
+ ] : [],
25176
+ outcome: turn.assistantText ? { status: "succeeded" } : {
25177
+ status: "unknown",
25178
+ reason: "ACP-only history does not expose the terminal stop reason"
25179
+ }
25180
+ }));
25181
+ }
25182
+ #recordAcpOnlyTurn(command, output) {
25183
+ const assistant = output.visibleAgentMessage();
25184
+ this.#acpOnlyTurns.push({
25185
+ id: command.turnId,
25186
+ text: command.input.map((part) => part.text).join("\n"),
25187
+ ...assistant ? { assistantText: assistant.text, itemId: assistant.itemId } : {}
25188
+ });
25189
+ try {
25190
+ writeCursorAcpOnlyTurns(this.#nativeSessionId, this.transport.options.environment, this.#acpOnlyTurns);
25191
+ } catch {
25192
+ }
24850
25193
  }
24851
25194
  /** Bound to unattended full access only; Cursor itself keeps enforcing its team policy. */
24852
25195
  #refuseUnattendedApproval() {
24853
25196
  this.#unattendedApproval = true;
24854
25197
  return this.transport.cancel();
24855
25198
  }
25199
+ #refuseMissingAllowOption() {
25200
+ this.#missingAllowOption = true;
25201
+ return this.transport.cancel();
25202
+ }
24856
25203
  #subagentConfiguration() {
24857
25204
  const state = this.initialState;
24858
25205
  const model2 = state.effectiveModel ? cursorModels(this.info).current : void 0;
@@ -24873,9 +25220,19 @@ var CursorSession = class {
24873
25220
  this.#configuring = true;
24874
25221
  let replay;
24875
25222
  try {
24876
- const history = () => readCursorNativeHistory(this.transport.sessionId, this.transport.options.cwd, this.transport.options.environment, this.#fresh);
25223
+ const acpOnly = this.#acpOnlyNativeStore();
25224
+ if (acpOnly)
25225
+ return {
25226
+ ok: true,
25227
+ value: {
25228
+ turns: this.#acpOnlySnapshotTurns(),
25229
+ state: structuredClone(this.initialState)
25230
+ }
25231
+ };
25232
+ const allowMissing = this.#fresh;
25233
+ const history = () => readCursorNativeHistory(this.transport.sessionId, this.transport.options.cwd, this.transport.options.environment, allowMissing);
24877
25234
  const before = history();
24878
- if (before.turns.length === 0 && this.#fresh)
25235
+ if (before.turns.length === 0 && allowMissing)
24879
25236
  return { ok: true, value: { turns: [], state: structuredClone(this.initialState) } };
24880
25237
  if (!this.#snapshot || !before.revision || this.#snapshot.revision !== before.revision) {
24881
25238
  replay = new CursorTransport({
@@ -24940,7 +25297,7 @@ var CursorSession = class {
24940
25297
  return rejected("invalidRequest", "Cursor requires nonempty text input");
24941
25298
  let before;
24942
25299
  try {
24943
- before = this.#native(this.#fresh);
25300
+ before = this.#acpOnlyNativeStore() ? [] : this.#native(this.#fresh);
24944
25301
  } catch (error51) {
24945
25302
  return { ok: false, error: cursorError(error51) };
24946
25303
  }
@@ -24962,8 +25319,44 @@ var CursorSession = class {
24962
25319
  } else if (command.type === "model.select") {
24963
25320
  selections = cursorModelSelection(this.info, command.model.id);
24964
25321
  } else {
24965
- if (!CURSOR_MODES.modes.some((mode) => mode.id === command.permissionModeId))
25322
+ const catalog = cursorHostPermissionModes(this.info, {
25323
+ ...this.initialState.effectivePermissionModeId ? { effectiveHostModeId: this.initialState.effectivePermissionModeId } : {}
25324
+ });
25325
+ if (!catalog.modes.some((mode) => mode.id === command.permissionModeId))
24966
25326
  return rejected("invalidRequest", "Unknown Cursor execution mode");
25327
+ const nativeOwnsHostId = cursorModes(this.info).modes.some((mode) => mode.id === HOST_RUN_EVERYTHING_MODE_ID);
25328
+ if (command.permissionModeId === HOST_RUN_EVERYTHING_MODE_ID && !nativeOwnsHostId) {
25329
+ if (!cursorNativeModeIsWritable(cursorCurrentPermissionModeId(this.info))) {
25330
+ const moved = await this.#applyNativeMode(cursorRunnableNativeModeId(this.info));
25331
+ if (!moved.ok)
25332
+ return moved;
25333
+ }
25334
+ this.#autoAllowNativeToolApprovals = this.#executionPolicy !== "unattended-full-access";
25335
+ this.initialState.effectivePermissionModeId = HOST_RUN_EVERYTHING_MODE_ID;
25336
+ this.#emitState();
25337
+ return { ok: true, value: { completed: true } };
25338
+ }
25339
+ if (cursorCurrentPermissionModeId(this.info) === command.permissionModeId) {
25340
+ this.#autoAllowNativeToolApprovals = false;
25341
+ this.initialState.effectivePermissionModeId = command.permissionModeId;
25342
+ this.#emitState();
25343
+ return { ok: true, value: { completed: true } };
25344
+ }
25345
+ if (cursorUsesNativeSessionModes(this.info)) {
25346
+ try {
25347
+ await this.transport.setMode(command.permissionModeId);
25348
+ } catch (error51) {
25349
+ return { ok: false, error: cursorError(error51) };
25350
+ }
25351
+ this.info.modes = {
25352
+ currentModeId: command.permissionModeId,
25353
+ availableModes: this.info.modes?.availableModes ?? []
25354
+ };
25355
+ this.#autoAllowNativeToolApprovals = false;
25356
+ this.initialState.effectivePermissionModeId = command.permissionModeId;
25357
+ this.#emitState();
25358
+ return { ok: true, value: { completed: true } };
25359
+ }
24967
25360
  selections = [["mode", command.permissionModeId]];
24968
25361
  }
24969
25362
  for (const [configId, value] of selections) {
@@ -24977,14 +25370,20 @@ var CursorSession = class {
24977
25370
  if (model2?.type === "select")
24978
25371
  this.initialState.effectiveModel = cursorConfiguredModelRef(model2.currentValue, result.configOptions);
24979
25372
  const mode = result.configOptions.find((option) => option.id === "mode");
24980
- if (mode)
24981
- this.initialState.effectivePermissionModeId = harnessPermissionModeIdSchema.parse(mode.currentValue);
25373
+ if (mode) {
25374
+ const nativeId = harnessPermissionModeIdSchema.parse(mode.currentValue);
25375
+ if (command.type === "permissionMode.select" || !cursorNativeModeIsWritable(nativeId)) {
25376
+ this.#autoAllowNativeToolApprovals = false;
25377
+ this.initialState.effectivePermissionModeId = nativeId;
25378
+ } else if (this.#autoAllowNativeToolApprovals) {
25379
+ this.initialState.effectivePermissionModeId = HOST_RUN_EVERYTHING_MODE_ID;
25380
+ } else {
25381
+ this.initialState.effectivePermissionModeId = nativeId;
25382
+ }
25383
+ }
24982
25384
  delete this.initialState.effectiveThinkingOptionId;
24983
25385
  Object.assign(this.initialState, cursorThinkingState(this.info));
24984
- this.#channel.emit({
24985
- kind: "event",
24986
- event: { type: "session.state.changed", state: structuredClone(this.initialState) }
24987
- });
25386
+ this.#emitState();
24988
25387
  }
24989
25388
  return { ok: true, value: { completed: true } };
24990
25389
  } catch (error51) {
@@ -25001,6 +25400,7 @@ var CursorSession = class {
25001
25400
  }), before.length);
25002
25401
  this.#subagentOutput = output.subagents;
25003
25402
  this.#unattendedApproval = false;
25403
+ this.#missingAllowOption = false;
25004
25404
  this.#channel.emit({ kind: "event", event: { type: "turn.started", turnId: command.turnId } });
25005
25405
  let outcome = {
25006
25406
  status: "failed",
@@ -25045,22 +25445,31 @@ var CursorSession = class {
25045
25445
  retryable: false
25046
25446
  }
25047
25447
  };
25448
+ else if (!this.#active?.cancelled && this.#missingAllowOption)
25449
+ outcome = {
25450
+ status: "failed",
25451
+ error: {
25452
+ code: "nativeFailure",
25453
+ message: "Cursor requested approval without an allow option during Run everything",
25454
+ retryable: false
25455
+ }
25456
+ };
25048
25457
  try {
25049
- const after = this.#native(this.#fresh);
25458
+ const acpOnly = this.#acpOnlyNativeStore();
25459
+ const after = acpOnly ? [] : this.#native(this.#fresh);
25050
25460
  const added = after.filter((turn) => !before.some((old) => old.id === turn.id));
25051
25461
  const administrative = command.input.map((part) => part.text).join("\n").trim().toLowerCase() === "/copy-request-id";
25052
25462
  if (administrative && JSON.stringify(before) === JSON.stringify(after)) {
25463
+ } else if (acpOnly && outcome.status === "succeeded" && output.hasVisibleAssistantText()) {
25464
+ nativeTurnRef = cursorNativeTurnRef(this.#nativeSessionId, command.turnId);
25465
+ this.#recordAcpOnlyTurn(command, output);
25466
+ this.#fresh = false;
25053
25467
  } else {
25054
25468
  const prompted = added.filter((turn) => !turn.notification);
25055
25469
  const own = prompted[0];
25056
25470
  if (!own || prompted.length !== 1 || after.length !== before.length + added.length || before.some((turn, index) => after[index]?.id !== turn.id) || own.text !== command.input.map((part) => part.text).join("\n"))
25057
25471
  throw new Error("Cursor terminal has no unique, verified native turn identity");
25058
- nativeTurnRef = nativeTurnRefSchema.parse({
25059
- harnessId: "cursor-cli",
25060
- nativeSessionId: this.transport.sessionId,
25061
- nativeTurnKey: own.id,
25062
- formatVersion: 1
25063
- });
25472
+ nativeTurnRef = cursorNativeTurnRef(this.#nativeSessionId, own.id);
25064
25473
  this.#fresh = false;
25065
25474
  const ownIndex = after.findIndex((turn) => turn.id === own.id);
25066
25475
  if (outcome.status === "succeeded" && cursorForkAvailable() && (ownIndex === after.length - 1 || after[ownIndex + 1]?.rewindRoot))
@@ -30231,6 +30231,13 @@ var GrokHarnessSession = class {
30231
30231
  const prompt = grokGoalPrompt(command.arguments);
30232
30232
  if (!prompt.ok)
30233
30233
  return prompt;
30234
+ if (prompt.value === "/goal status") {
30235
+ return this.#startTurn({
30236
+ type: "turn.start",
30237
+ turnId: command.turnId,
30238
+ input: [{ type: "text", text: prompt.value }]
30239
+ }, true);
30240
+ }
30234
30241
  const goals = this.goals;
30235
30242
  const goalControl = this.#transport.goalControl?.bind(this.#transport);
30236
30243
  if (goals && goalControl) {
package/bin/codexhost CHANGED
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karlorz/codexhost-cli-linux-x64",
3
- "version": "0.22.0-5",
3
+ "version": "0.22.0-7",
4
4
  "description": "Run Pi and Claude Code as first-class external harnesses inside Codex Desktop.",
5
5
  "type": "module",
6
6
  "files": [