@liberseek/boft-cli-win32-arm64 0.6.7 → 0.6.9

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.
@@ -14647,12 +14647,21 @@ var harnessAccountSnapshotSchema = external_exports.object({
14647
14647
  plan: external_exports.string().trim().min(1).max(128).optional(),
14648
14648
  credits: accountCreditsSnapshotSchema
14649
14649
  }).strict();
14650
- var harnessAccountListParamsSchema = external_exports.object({}).strict();
14650
+ var harnessAccountIdentityShape = {
14651
+ harnessId: harnessIdSchema,
14652
+ harnessName: external_exports.string().trim().min(1).max(128)
14653
+ };
14654
+ var harnessAccountSourceSchema = external_exports.object(harnessAccountIdentityShape).strict();
14655
+ var harnessAccountSourceListParamsSchema = external_exports.object({}).strict();
14656
+ var harnessAccountSourceListResultSchema = external_exports.object({ sources: external_exports.array(harnessAccountSourceSchema).max(128) }).strict();
14657
+ var harnessAccountInspectParamsSchema = external_exports.object({ harnessId: harnessIdSchema, refresh: external_exports.boolean().optional() }).strict();
14658
+ var harnessAccountInspectResultSchema = external_exports.object({
14659
+ ...harnessAccountIdentityShape,
14660
+ account: harnessAccountSnapshotSchema.nullable()
14661
+ }).strict();
14662
+ var harnessAccountListParamsSchema = external_exports.object({ refresh: external_exports.boolean().optional() }).strict();
14651
14663
  var harnessAccountListResultSchema = external_exports.object({
14652
- accounts: external_exports.array(harnessAccountSnapshotSchema.extend({
14653
- harnessId: harnessIdSchema,
14654
- harnessName: external_exports.string().min(1)
14655
- })).max(128)
14664
+ accounts: external_exports.array(harnessAccountSnapshotSchema.extend(harnessAccountIdentityShape)).max(128)
14656
14665
  }).strict();
14657
14666
 
14658
14667
  // ../../shared-contracts/dist/json-value.js
@@ -14948,7 +14957,6 @@ var threadInspectionParamsSchema = external_exports.object({
14948
14957
  }).strict();
14949
14958
  var codexThreadInspectionSchema = external_exports.object({
14950
14959
  owner: external_exports.literal("codex"),
14951
- accountId: nonBlankTextSchema2.optional(),
14952
14960
  locked: external_exports.literal(true)
14953
14961
  }).strict();
14954
14962
  var externalThreadInspectionSchema = external_exports.object({
@@ -15090,53 +15098,25 @@ var codexAccountSchema = external_exports.object({
15090
15098
  label: nonBlankTextSchema3.max(256),
15091
15099
  email: external_exports.string().email().max(320).optional(),
15092
15100
  planType: codexAccountPlanTypeSchema.optional(),
15093
- codexHome: nonBlankTextSchema3.max(16384),
15094
- active: external_exports.boolean(),
15095
- isDefault: external_exports.boolean(),
15096
15101
  authKind: codexAccountAuthKindSchema.optional(),
15097
15102
  authIdentity: nonBlankTextSchema3.max(256).optional()
15098
15103
  }).strict();
15099
- var codexAccountListResultSchema = external_exports.object({ accounts: external_exports.array(codexAccountSchema).max(128) }).strict();
15100
- var codexAccountCreateParamsSchema = external_exports.object({ label: nonBlankTextSchema3.max(256).optional() }).strict();
15101
- var codexAccountActivateParamsSchema = external_exports.object({ accountId: accountIdSchema }).strict();
15102
- var codexAccountDeleteParamsSchema = external_exports.object({ accountId: accountIdSchema }).strict();
15103
- var codexAccountDeleteResultSchema = external_exports.object({ deletedAccountId: accountIdSchema }).strict();
15104
- var codexAccountMutationResultSchema = external_exports.object({ account: codexAccountSchema }).strict();
15105
- var codexAccountLoginStartParamsSchema = external_exports.object({ accountId: accountIdSchema }).strict();
15106
- var codexAccountLoginStartResultSchema = external_exports.object({
15107
- accountId: accountIdSchema,
15108
- loginId: nonBlankTextSchema3.max(1024),
15109
- verificationUrl: external_exports.string().url().max(16384),
15110
- userCode: nonBlankTextSchema3.max(1024)
15111
- }).strict();
15112
- var codexAccountLoginCancelParamsSchema = external_exports.object({ accountId: accountIdSchema.optional(), loginId: nonBlankTextSchema3.max(1024) }).strict();
15113
- var codexAccountLoginCancelResultSchema = external_exports.object({ cancelled: external_exports.boolean() }).strict();
15114
- var codexAccountLoginCompletedSchema = external_exports.object({
15115
- accountId: accountIdSchema,
15116
- loginId: nonBlankTextSchema3.max(1024),
15117
- success: external_exports.boolean(),
15118
- error: external_exports.string().max(4096).nullable()
15104
+ var codexAccountPhaseSchema = external_exports.enum(["ready", "unavailable"]);
15105
+ var codexAccountListResultSchema = external_exports.object({
15106
+ version: external_exports.literal(2),
15107
+ currentAccountId: accountIdSchema.nullable(),
15108
+ phase: codexAccountPhaseSchema,
15109
+ revision: external_exports.number().int().nonnegative(),
15110
+ instanceId: nonBlankTextSchema3.max(1024).optional(),
15111
+ accounts: external_exports.array(codexAccountSchema).max(1)
15119
15112
  }).strict();
15120
- var codexAccountUsageParamsSchema = external_exports.object({ accountId: accountIdSchema }).strict();
15113
+ var codexAccountUsageParamsSchema = external_exports.object({ accountId: accountIdSchema, refresh: external_exports.boolean().optional() }).strict();
15121
15114
  var codexAccountUsageResultSchema = external_exports.object({
15122
15115
  accountId: accountIdSchema,
15123
15116
  usage: threadUsageSnapshotSchema.nullable(),
15124
- accountCredits: accountCreditsSnapshotSchema.optional()
15125
- }).strict();
15126
- var codexAccountResetCreditConsumeParamsSchema = external_exports.object({
15127
- accountId: accountIdSchema,
15128
- idempotencyKey: external_exports.string().uuid().optional()
15129
- }).strict();
15130
- var codexAccountResetCreditConsumeOutcomeSchema = external_exports.enum([
15131
- "reset",
15132
- "nothingToReset",
15133
- "noCredit",
15134
- "alreadyRedeemed"
15135
- ]);
15136
- var codexAccountResetCreditConsumeResultSchema = external_exports.object({
15137
- accountId: accountIdSchema,
15138
- outcome: codexAccountResetCreditConsumeOutcomeSchema,
15139
- accountCredits: accountCreditsSnapshotSchema.optional()
15117
+ accountCredits: accountCreditsSnapshotSchema.optional(),
15118
+ freshness: external_exports.enum(["live", "cached"]),
15119
+ observedAt: external_exports.string().datetime().nullable()
15140
15120
  }).strict();
15141
15121
 
15142
15122
  // ../../shared-contracts/dist/harness-session-import.js
@@ -15645,7 +15625,7 @@ var harnessBrokerServerFrameSchema = external_exports.union([
15645
15625
  var harnessBrokerDescriptorSchema = external_exports.object({
15646
15626
  schemaVersion: external_exports.literal(1),
15647
15627
  protocolVersion: external_exports.literal(HARNESS_BROKER_PROTOCOL_VERSION),
15648
- harnessId: external_exports.literal("claude-code"),
15628
+ harnessId: harnessPluginIdSchema,
15649
15629
  generation: external_exports.string().uuid(),
15650
15630
  ownerPid: external_exports.number().int().positive(),
15651
15631
  socketPath: external_exports.string().min(1).max(512),
@@ -15700,22 +15680,34 @@ function defaultHarnessBrokerDirectory(environment = process.env) {
15700
15680
  const home = environment.HOME || os.homedir();
15701
15681
  return path.join(home, ".codexhost", "harness-broker");
15702
15682
  }
15703
- function defaultHarnessBrokerDescriptorPath(environment = process.env) {
15683
+ function defaultHarnessBrokerDescriptorPath(environment = process.env, harnessId = "claude-code") {
15684
+ harnessPluginIdSchema.parse(harnessId);
15685
+ if (harnessId !== "claude-code")
15686
+ return path.join(defaultHarnessBrokerDirectory(environment), `${harnessId}-broker-v1.json`);
15704
15687
  return environment[HARNESS_BROKER_DESCRIPTOR_ENV] ?? path.join(defaultHarnessBrokerDirectory(environment), HARNESS_BROKER_DESCRIPTOR_FILE);
15705
15688
  }
15706
15689
 
15707
15690
  // ../../harness-broker/dist/validation.js
15708
15691
  var cwdSchema = external_exports.string().min(1).max(16384);
15692
+ var brokerEnvironmentSchema = external_exports.object({
15693
+ CODEXHOST_CLI_PATH: external_exports.string().max(16384).optional(),
15694
+ CODEXHOST_RUNTIME_ENDPOINT: external_exports.string().max(16384).optional(),
15695
+ CODEXHOST_RUNTIME_TOKEN: external_exports.string().max(16384).optional(),
15696
+ CODEXHOST_THREAD_ID: external_exports.string().max(256).optional()
15697
+ }).strict();
15709
15698
  var createSchema = external_exports.object({
15710
15699
  kind: external_exports.literal("create"),
15711
15700
  cwd: cwdSchema,
15712
15701
  executionPolicy: external_exports.enum(["default", "unattended-full-access"]).optional(),
15702
+ environment: brokerEnvironmentSchema.optional(),
15713
15703
  model: harnessModelRefSchema.optional(),
15714
15704
  thinkingOptionId: harnessThinkingOptionIdSchema.optional(),
15715
15705
  permissionModeId: harnessPermissionModeIdSchema.optional()
15716
15706
  }).strict();
15717
15707
  var resumeSchema = external_exports.object({
15718
15708
  kind: external_exports.literal("resume"),
15709
+ environment: brokerEnvironmentSchema.optional(),
15710
+ permissionModeId: harnessPermissionModeIdSchema.optional(),
15719
15711
  model: harnessModelRefSchema.optional(),
15720
15712
  thinkingOptionId: harnessThinkingOptionIdSchema.optional(),
15721
15713
  nativeRef: nativeSessionRefSchema,
@@ -15724,12 +15716,14 @@ var resumeSchema = external_exports.object({
15724
15716
  }).strict();
15725
15717
  var forkSchema = external_exports.object({
15726
15718
  kind: external_exports.literal("fork"),
15719
+ environment: brokerEnvironmentSchema.optional(),
15727
15720
  sourceRef: nativeSessionRefSchema,
15728
15721
  checkpoint: nativeCheckpointRefSchema,
15729
15722
  cwd: cwdSchema
15730
15723
  }).strict();
15731
15724
  var rollbackSchema = external_exports.object({
15732
15725
  kind: external_exports.literal("rollbackLastTurn"),
15726
+ environment: brokerEnvironmentSchema.optional(),
15733
15727
  model: harnessModelRefSchema.optional(),
15734
15728
  thinkingOptionId: harnessThinkingOptionIdSchema.optional(),
15735
15729
  permissionModeId: harnessPermissionModeIdSchema.optional(),
@@ -15990,37 +15984,43 @@ function parseSessionMetadata(value) {
15990
15984
  async function readDescriptor(descriptorPath) {
15991
15985
  const metadata = await lstat(descriptorPath);
15992
15986
  if (metadata.isSymbolicLink())
15993
- throw new Error("Claude Aqua broker descriptor must not be a symlink");
15987
+ throw new Error("Aqua Harness broker descriptor must not be a symlink");
15994
15988
  if (!metadata.isFile())
15995
- throw new Error("Claude Aqua broker descriptor is not a file");
15989
+ throw new Error("Aqua Harness broker descriptor is not a file");
15996
15990
  if (process.platform !== "win32") {
15997
15991
  if ((metadata.mode & 63) !== 0)
15998
- throw new Error("Claude Aqua broker descriptor is not owner-only");
15992
+ throw new Error("Aqua Harness broker descriptor is not owner-only");
15999
15993
  if (process.getuid && metadata.uid !== process.getuid()) {
16000
- throw new Error("Claude Aqua broker descriptor belongs to another user");
15994
+ throw new Error("Aqua Harness broker descriptor belongs to another user");
16001
15995
  }
16002
15996
  }
16003
15997
  const descriptor = harnessBrokerDescriptorSchema.parse(JSON.parse(await readFile(descriptorPath, "utf8")));
16004
15998
  if (process.platform === "darwin" && Buffer.byteLength(descriptor.socketPath) > 103) {
16005
- throw new Error("Claude Aqua broker socket path is too long for macOS");
15999
+ throw new Error("Aqua Harness broker socket path is too long for macOS");
16006
16000
  }
16007
16001
  if (process.platform !== "win32") {
16008
16002
  const socket = await lstat(descriptor.socketPath);
16009
16003
  if (socket.isSymbolicLink() || !socket.isSocket()) {
16010
- throw new Error("Claude Aqua broker endpoint is not a Unix socket");
16004
+ throw new Error("Aqua Harness broker endpoint is not a Unix socket");
16011
16005
  }
16012
16006
  if ((socket.mode & 63) !== 0 || process.getuid && socket.uid !== process.getuid()) {
16013
- throw new Error("Claude Aqua broker endpoint is not owner-only");
16007
+ throw new Error("Aqua Harness broker endpoint is not owner-only");
16014
16008
  }
16015
16009
  }
16016
16010
  try {
16017
16011
  process.kill(descriptor.ownerPid, 0);
16018
16012
  } catch {
16019
- throw new Error("Claude Aqua broker owner process is unavailable");
16013
+ throw new Error("Aqua Harness broker owner process is unavailable");
16020
16014
  }
16021
16015
  return descriptor;
16022
16016
  }
16023
16017
  var BrokerConnection = class _BrokerConnection {
16018
+ #onClose;
16019
+ onClose(callback) {
16020
+ this.#onClose = callback;
16021
+ if (this.#closed)
16022
+ callback();
16023
+ }
16024
16024
  #descriptor;
16025
16025
  #socket;
16026
16026
  #pending = /* @__PURE__ */ new Map();
@@ -16033,14 +16033,16 @@ var BrokerConnection = class _BrokerConnection {
16033
16033
  this.#descriptor = descriptor;
16034
16034
  this.#socket = socket;
16035
16035
  consumeBrokerFrames(socket, (raw) => this.#frame(raw), (error52) => this.#fail(error52));
16036
- socket.once("close", () => this.#fail(new Error("Claude Aqua broker connection closed")));
16036
+ socket.once("close", () => this.#fail(new Error("Aqua Harness broker connection closed")));
16037
16037
  socket.once("error", (error52) => this.#fail(error52));
16038
16038
  }
16039
- static async connect(descriptorPath) {
16039
+ static async connect(descriptorPath, harnessId) {
16040
16040
  const descriptor = await readDescriptor(descriptorPath);
16041
+ if (descriptor.harnessId !== harnessId)
16042
+ throw new Error("Aqua broker belongs to another Harness");
16041
16043
  const socket = net.createConnection(descriptor.socketPath);
16042
16044
  await new Promise((resolve2, reject) => {
16043
- const timeout = setTimeout(() => reject(new Error("Timed out connecting to Claude Aqua broker")), HARNESS_BROKER_REQUEST_TIMEOUT_MS);
16045
+ const timeout = setTimeout(() => reject(new Error("Timed out connecting to Aqua Harness broker")), HARNESS_BROKER_REQUEST_TIMEOUT_MS);
16044
16046
  socket.once("connect", () => {
16045
16047
  clearTimeout(timeout);
16046
16048
  resolve2();
@@ -16067,29 +16069,38 @@ var BrokerConnection = class _BrokerConnection {
16067
16069
  const id = "__hello__";
16068
16070
  const timeout = setTimeout(() => {
16069
16071
  this.#pending.delete(id);
16070
- reject(new Error("Claude Aqua broker authentication timed out"));
16072
+ reject(new Error("Aqua Harness broker authentication timed out"));
16071
16073
  }, HARNESS_BROKER_REQUEST_TIMEOUT_MS);
16072
16074
  this.#pending.set(id, { resolve: () => resolve2(), reject, timeout });
16073
16075
  });
16074
16076
  }
16075
16077
  register(session) {
16078
+ if (this.#closed)
16079
+ throw new Error("Aqua Harness broker connection closed while opening Session");
16076
16080
  this.#sessions.set(session.sessionId, session);
16077
16081
  }
16082
+ get closed() {
16083
+ return this.#closed;
16084
+ }
16078
16085
  unregister(sessionId) {
16079
16086
  this.#sessions.delete(sessionId);
16080
16087
  }
16088
+ async dispose() {
16089
+ await Promise.allSettled([...this.#sessions.values()].map((session) => session.close()));
16090
+ this.close();
16091
+ }
16081
16092
  async request(method, params) {
16082
16093
  if (this.#closed)
16083
- throw new Error("Claude Aqua broker connection is closed");
16094
+ throw new Error("Aqua Harness broker connection is closed");
16084
16095
  if (this.#pending.size >= HARNESS_BROKER_MAX_PENDING_REQUESTS) {
16085
- throw new Error("Claude Aqua broker request limit exceeded");
16096
+ throw new Error("Aqua Harness broker request limit exceeded");
16086
16097
  }
16087
16098
  this.#inputSequence += 1;
16088
16099
  const id = randomUUID();
16089
16100
  const response = new Promise((resolve2, reject) => {
16090
16101
  const timeout = setTimeout(() => {
16091
16102
  this.#pending.delete(id);
16092
- reject(new Error(`Claude Aqua broker ${method} timed out`));
16103
+ reject(new Error(`Aqua Harness broker ${method} timed out`));
16093
16104
  }, HARNESS_BROKER_REQUEST_TIMEOUT_MS);
16094
16105
  this.#pending.set(id, { resolve: resolve2, reject, timeout });
16095
16106
  });
@@ -16109,12 +16120,14 @@ var BrokerConnection = class _BrokerConnection {
16109
16120
  return;
16110
16121
  this.#closed = true;
16111
16122
  this.#socket.destroy();
16112
- this.#fail(new Error("Claude Aqua broker connection closed"));
16123
+ this.#fail(new Error("Aqua Harness broker connection closed"));
16113
16124
  }
16114
16125
  #frame(raw) {
16126
+ if (this.#closed)
16127
+ return;
16115
16128
  const frame = harnessBrokerServerFrameSchema.parse(raw);
16116
16129
  if (frame.generation !== this.#descriptor.generation || frame.sequence !== this.#outputSequence + 1) {
16117
- this.#fail(new Error("Claude Aqua broker response generation or sequence is invalid"));
16130
+ this.#fail(new Error("Aqua Harness broker response generation or sequence is invalid"));
16118
16131
  return;
16119
16132
  }
16120
16133
  this.#outputSequence = frame.sequence;
@@ -16136,7 +16149,7 @@ var BrokerConnection = class _BrokerConnection {
16136
16149
  }
16137
16150
  const pending = this.#pending.get(frame.id);
16138
16151
  if (!pending) {
16139
- this.#fail(new Error("Claude Aqua broker returned an unknown response ID"));
16152
+ this.#fail(new Error("Aqua Harness broker returned an unknown response ID"));
16140
16153
  return;
16141
16154
  }
16142
16155
  clearTimeout(pending.timeout);
@@ -16152,6 +16165,8 @@ var BrokerConnection = class _BrokerConnection {
16152
16165
  this.#failed = true;
16153
16166
  if (!this.#closed)
16154
16167
  this.#closed = true;
16168
+ this.#socket.destroy();
16169
+ this.#onClose?.();
16155
16170
  for (const pending of this.#pending.values()) {
16156
16171
  clearTimeout(pending.timeout);
16157
16172
  pending.reject(error52);
@@ -16162,7 +16177,10 @@ var BrokerConnection = class _BrokerConnection {
16162
16177
  }
16163
16178
  };
16164
16179
  var BrokeredHarnessSession = class {
16165
- harnessId = harnessIdSchema.parse("claude-code");
16180
+ openInput;
16181
+ reconnect;
16182
+ onClose;
16183
+ harnessId;
16166
16184
  outputs;
16167
16185
  #channel = new HarnessOutputChannel();
16168
16186
  #connection;
@@ -16170,9 +16188,18 @@ var BrokeredHarnessSession = class {
16170
16188
  #metadata;
16171
16189
  #faulted = false;
16172
16190
  #closed = false;
16173
- constructor(connection, metadata) {
16191
+ #state;
16192
+ #recovery;
16193
+ constructor(connection, metadata, harnessId, openInput, reconnect, onClose) {
16194
+ this.openInput = openInput;
16195
+ this.reconnect = reconnect;
16196
+ this.onClose = onClose;
16197
+ this.harnessId = harnessId;
16198
+ if (metadata.initialState.nativeRef && metadata.initialState.nativeRef.harnessId !== harnessId)
16199
+ throw new Error("Broker Session identity belongs to another Harness");
16174
16200
  this.#connection = connection;
16175
16201
  this.#metadata = metadata;
16202
+ this.#state = structuredClone(metadata.initialState);
16176
16203
  this.outputs = this.#channel.outputs;
16177
16204
  if (metadata.commands) {
16178
16205
  this.commands = {
@@ -16216,6 +16243,10 @@ var BrokeredHarnessSession = class {
16216
16243
  if (this.#closed || generation !== this.#metadata.sessionGeneration)
16217
16244
  return;
16218
16245
  const value = harnessOutputSchema.parse(output);
16246
+ if (value.kind === "event" && value.event.type === "session.state.changed") {
16247
+ const nativeRef = value.event.state.nativeRef ?? this.#state.nativeRef;
16248
+ this.#state = { ...structuredClone(value.event.state), ...nativeRef ? { nativeRef } : {} };
16249
+ }
16219
16250
  if (value.kind === "event" && value.event.type === "session.faulted" || isAuthenticationTerminal(value)) {
16220
16251
  this.#faulted = true;
16221
16252
  }
@@ -16234,6 +16265,13 @@ var BrokeredHarnessSession = class {
16234
16265
  await this.#request("session.refreshUsage", {});
16235
16266
  }
16236
16267
  async readSnapshot() {
16268
+ if (this.#closed)
16269
+ return { ok: false, error: unavailable("Aqua Harness broker Session is closed", false) };
16270
+ if (this.#connection.closed) {
16271
+ const recovered = await this.#ensureRecovery();
16272
+ if (!recovered.ok)
16273
+ return recovered;
16274
+ }
16237
16275
  try {
16238
16276
  return parseHarnessResult(await this.#request("session.readSnapshot", {}));
16239
16277
  } catch (error52) {
@@ -16245,20 +16283,11 @@ var BrokeredHarnessSession = class {
16245
16283
  }
16246
16284
  async execute(command) {
16247
16285
  if (this.#closed)
16248
- return { ok: false, error: unavailable("Claude Aqua broker Session is closed", false) };
16286
+ return { ok: false, error: unavailable("Aqua Harness broker Session is closed", false) };
16249
16287
  if (this.#faulted && command.type === "turn.start") {
16250
- try {
16251
- const reopened = parseHarnessResult(await this.#request("session.reopen", {}));
16252
- if (!reopened.ok)
16253
- return reopened;
16254
- this.#metadata = parseSessionMetadata(reopened.value);
16255
- this.#faulted = false;
16256
- } catch (error52) {
16257
- return {
16258
- ok: false,
16259
- error: unavailable(error52 instanceof Error ? error52.message : String(error52))
16260
- };
16261
- }
16288
+ const recovered = await this.#ensureRecovery();
16289
+ if (!recovered.ok)
16290
+ return recovered;
16262
16291
  }
16263
16292
  try {
16264
16293
  return parseHarnessResult(await this.#request("session.execute", { command }));
@@ -16273,9 +16302,69 @@ var BrokeredHarnessSession = class {
16273
16302
  if (this.#closed)
16274
16303
  return;
16275
16304
  this.#closed = true;
16305
+ await this.#recovery?.catch(() => {
16306
+ });
16276
16307
  this.#connection.unregister(this.sessionId);
16277
16308
  await this.#request("session.close", {}).catch(() => void 0);
16278
16309
  this.#channel.end();
16310
+ this.onClose();
16311
+ }
16312
+ async #ensureRecovery() {
16313
+ try {
16314
+ this.#recovery ??= this.#recover().finally(() => {
16315
+ this.#recovery = void 0;
16316
+ });
16317
+ return await this.#recovery;
16318
+ } catch (error52) {
16319
+ return {
16320
+ ok: false,
16321
+ error: unavailable(error52 instanceof Error ? error52.message : String(error52))
16322
+ };
16323
+ }
16324
+ }
16325
+ async #recover() {
16326
+ const previous = this.#connection;
16327
+ const connection = previous.closed ? await this.reconnect() : previous;
16328
+ const nativeRef = this.#state.nativeRef;
16329
+ if (!nativeRef)
16330
+ return {
16331
+ ok: false,
16332
+ error: unavailable("Cannot recover a Session without confirmed native identity", false)
16333
+ };
16334
+ const result = parseHarnessResult(await (connection === previous ? this.#request("session.reopen", {}) : connection.request("adapter.open", {
16335
+ kind: "resume",
16336
+ cwd: this.openInput.cwd,
16337
+ nativeRef,
16338
+ ...this.openInput.environment ? { environment: this.openInput.environment } : {},
16339
+ ...this.#state.effectiveModel ? { model: this.#state.effectiveModel } : {},
16340
+ ...this.#state.effectiveThinkingOptionId ? { thinkingOptionId: this.#state.effectiveThinkingOptionId } : {},
16341
+ ...this.#state.effectivePermissionModeId ? { permissionModeId: this.#state.effectivePermissionModeId } : {}
16342
+ })));
16343
+ if (!result.ok)
16344
+ return result;
16345
+ const metadata = parseSessionMetadata(result.value), observed = metadata.initialState.nativeRef;
16346
+ if (this.#closed || connection.closed || !observed || observed.harnessId !== nativeRef.harnessId || observed.nativeSessionId !== nativeRef.nativeSessionId || observed.formatVersion !== nativeRef.formatVersion) {
16347
+ await connection.request("session.close", {
16348
+ sessionId: metadata.sessionId,
16349
+ sessionGeneration: metadata.sessionGeneration
16350
+ }).catch(() => {
16351
+ });
16352
+ return {
16353
+ ok: false,
16354
+ error: unavailable("Broker recovery did not preserve the live native Session identity", false)
16355
+ };
16356
+ }
16357
+ previous.unregister(this.sessionId);
16358
+ this.#connection = connection;
16359
+ this.#metadata = metadata;
16360
+ this.#state = structuredClone(metadata.initialState);
16361
+ connection.register(this);
16362
+ this.#faulted = false;
16363
+ this.#channel.emit({
16364
+ kind: "event",
16365
+ event: { type: "session.state.changed", state: structuredClone(this.#state) }
16366
+ });
16367
+ return { ok: true, value: void 0 };
16279
16368
  }
16280
16369
  #request(method, extra) {
16281
16370
  return this.#connection.request(method, {
@@ -16286,9 +16375,11 @@ var BrokeredHarnessSession = class {
16286
16375
  }
16287
16376
  };
16288
16377
  var BrokeredHarnessAdapter = class {
16378
+ #sessions = /* @__PURE__ */ new Set();
16289
16379
  commandCatalog;
16290
- harnessId = harnessIdSchema.parse("claude-code");
16380
+ harnessId;
16291
16381
  #descriptorPath;
16382
+ #forwardEnvironment;
16292
16383
  #connection = null;
16293
16384
  #closed = false;
16294
16385
  #sessionImportListing = null;
@@ -16383,9 +16474,11 @@ var BrokeredHarnessAdapter = class {
16383
16474
  }
16384
16475
  }
16385
16476
  constructor(input = {}) {
16477
+ this.harnessId = harnessPluginIdSchema.parse(input.harnessId ?? "claude-code");
16478
+ this.#forwardEnvironment = input.forwardDelegationEnvironment === true;
16386
16479
  if (input.commandCatalog)
16387
16480
  this.commandCatalog = input.commandCatalog;
16388
- this.#descriptorPath = input.descriptorPath ?? defaultHarnessBrokerDescriptorPath(input.environment);
16481
+ this.#descriptorPath = input.descriptorPath ?? defaultHarnessBrokerDescriptorPath(input.environment, this.harnessId);
16389
16482
  }
16390
16483
  async inspectAccount() {
16391
16484
  if (this.#closed)
@@ -16399,7 +16492,7 @@ var BrokeredHarnessAdapter = class {
16399
16492
  }
16400
16493
  async inspect(input = {}) {
16401
16494
  if (this.#closed)
16402
- return failedInspection("Claude Aqua broker adapter is closed");
16495
+ return failedInspection("Aqua Harness broker adapter is closed");
16403
16496
  try {
16404
16497
  return harnessInspectionSchema.parse(await (await this.#connect()).request("adapter.inspect", input));
16405
16498
  } catch (error52) {
@@ -16409,18 +16502,33 @@ var BrokeredHarnessAdapter = class {
16409
16502
  }
16410
16503
  async open(input) {
16411
16504
  if (this.#closed)
16412
- return { ok: false, error: unavailable("Claude Aqua broker adapter is closed", false) };
16505
+ return { ok: false, error: unavailable("Aqua Harness broker adapter is closed", false) };
16506
+ let connection;
16413
16507
  try {
16414
16508
  const safeInput = { ...input };
16415
16509
  delete safeInput.environment;
16416
- const result = parseHarnessResult(await (await this.#connect()).request("adapter.open", safeInput));
16510
+ if (this.#forwardEnvironment && input.environment) {
16511
+ const allowed2 = [
16512
+ "CODEXHOST_CLI_PATH",
16513
+ "CODEXHOST_RUNTIME_ENDPOINT",
16514
+ "CODEXHOST_RUNTIME_TOKEN",
16515
+ "CODEXHOST_THREAD_ID"
16516
+ ];
16517
+ const environment = Object.fromEntries(Object.entries(input.environment).filter(([key, value]) => allowed2.includes(key) && value !== void 0));
16518
+ if (Object.keys(environment).length)
16519
+ safeInput.environment = environment;
16520
+ }
16521
+ connection = await this.#connect();
16522
+ const result = parseHarnessResult(await connection.request("adapter.open", safeInput));
16417
16523
  if (!result.ok)
16418
16524
  return result;
16419
- return {
16420
- ok: true,
16421
- value: new BrokeredHarnessSession(await this.#connect(), parseSessionMetadata(result.value))
16422
- };
16525
+ const session = new BrokeredHarnessSession(connection, parseSessionMetadata(result.value), this.harnessId, safeInput, () => this.#connect(), () => {
16526
+ this.#sessions.delete(session);
16527
+ });
16528
+ this.#sessions.add(session);
16529
+ return { ok: true, value: session };
16423
16530
  } catch (error52) {
16531
+ connection?.close();
16424
16532
  this.#connection = null;
16425
16533
  return {
16426
16534
  ok: false,
@@ -16430,13 +16538,29 @@ var BrokeredHarnessAdapter = class {
16430
16538
  }
16431
16539
  async close() {
16432
16540
  this.#closed = true;
16541
+ await Promise.allSettled([...this.#sessions].map((session) => session.close()));
16542
+ this.#sessions.clear();
16433
16543
  const connection = await this.#connection?.catch(() => null);
16434
- connection?.close();
16544
+ await connection?.dispose();
16435
16545
  this.#connection = null;
16436
16546
  }
16437
16547
  #connect() {
16438
- if (!this.#connection)
16439
- this.#connection = BrokerConnection.connect(this.#descriptorPath);
16548
+ if (this.#closed)
16549
+ return Promise.reject(new Error("Aqua Harness broker adapter is closed"));
16550
+ if (!this.#connection) {
16551
+ const pending = BrokerConnection.connect(this.#descriptorPath, this.harnessId).then((connection) => {
16552
+ connection.onClose(() => {
16553
+ if (this.#connection === pending)
16554
+ this.#connection = null;
16555
+ });
16556
+ return connection;
16557
+ }).catch((error52) => {
16558
+ if (this.#connection === pending)
16559
+ this.#connection = null;
16560
+ throw error52;
16561
+ });
16562
+ this.#connection = pending;
16563
+ }
16440
16564
  return this.#connection;
16441
16565
  }
16442
16566
  };
@@ -45087,6 +45211,16 @@ function allowed(toolUseId, input, suggestions) {
45087
45211
  ...suggestions ? { updatedPermissions: suggestions } : {}
45088
45212
  };
45089
45213
  }
45214
+ function canDeliverSettlementImmediately(event, pendingEvents) {
45215
+ if (event.type !== "subagent.settled")
45216
+ return false;
45217
+ return !pendingEvents.some((pending) => {
45218
+ if (pending.type !== "subagent.started" && pending.type !== "subagent.updated" && pending.type !== "subagent.completed") {
45219
+ return false;
45220
+ }
45221
+ return event.callId !== void 0 && pending.callId === event.callId || pending.nativeSubagentId === event.nativeSubagentId;
45222
+ });
45223
+ }
45090
45224
  var ClaudeSdkTransport = class {
45091
45225
  sessionId;
45092
45226
  #children = [];
@@ -45108,6 +45242,7 @@ var ClaudeSdkTransport = class {
45108
45242
  #autonomous = null;
45109
45243
  #autonomousTurnHandler = null;
45110
45244
  #idleHandler = null;
45245
+ #threadEventHandler = null;
45111
45246
  #idleLive = false;
45112
45247
  #idleAccumulator = null;
45113
45248
  #closePromise = null;
@@ -45140,6 +45275,9 @@ var ClaudeSdkTransport = class {
45140
45275
  setIdleTurnHandler(handler) {
45141
45276
  this.#idleHandler = handler;
45142
45277
  }
45278
+ setThreadEventHandler(handler) {
45279
+ this.#threadEventHandler = handler;
45280
+ }
45143
45281
  setIdleLive(live) {
45144
45282
  this.#idleLive = live;
45145
45283
  if (!live)
@@ -45528,8 +45666,13 @@ var ClaudeSdkTransport = class {
45528
45666
  autonomous.nativeTurnKey = message.uuid;
45529
45667
  }
45530
45668
  const interpreted = autonomous.accumulator.consume(message);
45531
- for (const event of interpreted.events)
45669
+ for (const event of interpreted.events) {
45670
+ if (this.#threadEventHandler && canDeliverSettlementImmediately(event, autonomous.events)) {
45671
+ this.#threadEventHandler(event);
45672
+ continue;
45673
+ }
45532
45674
  autonomous.events.push({ ...event, observedAtMs });
45675
+ }
45533
45676
  if (interpreted.terminal) {
45534
45677
  this.#autonomous = null;
45535
45678
  const nativeTurnKey = autonomous.nativeTurnKey ?? `autonomous-${Date.now()}`;
@@ -46673,6 +46816,7 @@ var ClaudeHarnessSession = class {
46673
46816
  agentMessageStartedAtMs: null,
46674
46817
  reasoningStartedAtMs: /* @__PURE__ */ new Map(),
46675
46818
  held: false,
46819
+ rootSegmentActive: true,
46676
46820
  completion,
46677
46821
  resolveCompletion
46678
46822
  };
@@ -46781,6 +46925,7 @@ var ClaudeHarnessSession = class {
46781
46925
  agentMessageStartedAtMs: null,
46782
46926
  reasoningStartedAtMs: /* @__PURE__ */ new Map(),
46783
46927
  held: false,
46928
+ rootSegmentActive: true,
46784
46929
  completion,
46785
46930
  resolveCompletion
46786
46931
  };
@@ -47198,11 +47343,21 @@ var ClaudeHarnessSession = class {
47198
47343
  });
47199
47344
  this.#transport = transport;
47200
47345
  transport.setAutonomousTurnHandler((turn) => this.#handleAutonomousTurn(turn));
47346
+ transport.setThreadEventHandler((event) => {
47347
+ if (event.type === "subagent.settled") {
47348
+ this.#settleBackgroundSubagent(event.status, event.nativeSubagentId, event.callId, event.resultSummary);
47349
+ }
47350
+ });
47201
47351
  transport.setIdleTurnHandler({
47202
47352
  onEvent: (event) => {
47203
47353
  const active = this.#active;
47204
- if (active)
47354
+ if (active) {
47205
47355
  this.#handleTurnEvent(active, event);
47356
+ return;
47357
+ }
47358
+ if (event.type === "subagent.settled") {
47359
+ this.#settleBackgroundSubagent(event.status, event.nativeSubagentId, event.callId, event.resultSummary);
47360
+ }
47206
47361
  },
47207
47362
  onTerminal: (result) => {
47208
47363
  const active = this.#active;
@@ -47284,12 +47439,14 @@ var ClaudeHarnessSession = class {
47284
47439
  return;
47285
47440
  switch (event.type) {
47286
47441
  case "segment.started":
47287
- this.#observeRootOutput();
47442
+ this.#observeRootOutput(active);
47288
47443
  return;
47289
47444
  case "subagents.live":
47290
47445
  this.#occupancy.observeLive(event.nativeSubagentIds);
47446
+ this.#armContinuationQuiescence(active);
47291
47447
  return;
47292
47448
  case "compaction.started":
47449
+ this.#observeRootOutput(active);
47293
47450
  this.#startCompaction(active);
47294
47451
  return;
47295
47452
  case "compaction.completed":
@@ -47297,12 +47454,12 @@ var ClaudeHarnessSession = class {
47297
47454
  return;
47298
47455
  case "text.delta":
47299
47456
  if (event.delta.length > 0)
47300
- this.#observeRootOutput();
47457
+ this.#observeRootOutput(active);
47301
47458
  this.#appendText(active, event.messageId, event.delta);
47302
47459
  return;
47303
47460
  case "reasoning.delta":
47304
47461
  if (event.delta.length > 0)
47305
- this.#observeRootOutput();
47462
+ this.#observeRootOutput(active);
47306
47463
  this.#activateAssistantMessage(active, event.messageId);
47307
47464
  this.#appendReasoning(active, event.messageId, event.delta);
47308
47465
  return;
@@ -47310,6 +47467,7 @@ var ClaudeHarnessSession = class {
47310
47467
  this.#completeReasoning(active, event.messageId, { status: "succeeded" });
47311
47468
  return;
47312
47469
  case "message.completed": {
47470
+ this.#observeRootOutput(active);
47313
47471
  if (event.lastRequestUsage) {
47314
47472
  this.#requestUsageBoundary += 1;
47315
47473
  this.#applyLatestRequestUsage(active, event.lastRequestUsage);
@@ -47326,7 +47484,7 @@ var ClaudeHarnessSession = class {
47326
47484
  return;
47327
47485
  }
47328
47486
  case "tool.started":
47329
- this.#observeRootOutput();
47487
+ this.#observeRootOutput(active);
47330
47488
  for (const messageId of [...active.reasoningItems.keys()]) {
47331
47489
  this.#completeReasoning(active, messageId, { status: "succeeded" });
47332
47490
  }
@@ -47340,7 +47498,7 @@ var ClaudeHarnessSession = class {
47340
47498
  active.tools.complete(active.command.turnId, event, active.cancellationRequested);
47341
47499
  return;
47342
47500
  case "subagent.started":
47343
- this.#observeRootOutput();
47501
+ this.#observeRootOutput(active);
47344
47502
  for (const messageId of [...active.reasoningItems.keys()]) {
47345
47503
  this.#completeReasoning(active, messageId, { status: "succeeded" });
47346
47504
  }
@@ -47389,6 +47547,7 @@ var ClaudeHarnessSession = class {
47389
47547
  return;
47390
47548
  }
47391
47549
  case "interaction.requested":
47550
+ this.#observeRootOutput(active);
47392
47551
  this.#startInteraction(active, event.request);
47393
47552
  return;
47394
47553
  case "interaction.closed":
@@ -47690,6 +47849,7 @@ var ClaudeHarnessSession = class {
47690
47849
  agentMessageStartedAtMs: null,
47691
47850
  reasoningStartedAtMs: /* @__PURE__ */ new Map(),
47692
47851
  held: false,
47852
+ rootSegmentActive: true,
47693
47853
  completion,
47694
47854
  resolveCompletion
47695
47855
  };
@@ -47728,6 +47888,9 @@ var ClaudeHarnessSession = class {
47728
47888
  }
47729
47889
  #settleBackgroundSubagent(status, nativeSubagentId2, callId, resultSummary) {
47730
47890
  this.#occupancy.notify(callId, nativeSubagentId2);
47891
+ const active = this.#active;
47892
+ if (active?.held)
47893
+ this.#armContinuationQuiescence(active);
47731
47894
  if (!nativeSubagentId2)
47732
47895
  return;
47733
47896
  this.#event({
@@ -47745,6 +47908,7 @@ var ClaudeHarnessSession = class {
47745
47908
  #finishResult(active, result) {
47746
47909
  if (this.#active !== active || this.#hardCancelTask)
47747
47910
  return;
47911
+ active.rootSegmentActive = false;
47748
47912
  if (result.status === "succeeded" && (active.tools.size > 0 || active.subagents.size > 0)) {
47749
47913
  this.#finishFailed(active, transportFailure("protocol"));
47750
47914
  } else if (result.status === "succeeded") {
@@ -48022,13 +48186,15 @@ var ClaudeHarnessSession = class {
48022
48186
  }
48023
48187
  /** Completes held work after a quiet period with no further Root output. */
48024
48188
  #armContinuationQuiescence(active) {
48025
- this.#clearContinuationQuiescence();
48026
- if (!this.#occupancy.awaitingContinuation)
48189
+ if (this.#active !== active || !active.held || active.rootSegmentActive || this.#phase !== "open" || !this.#occupancy.awaitingContinuation) {
48027
48190
  return;
48191
+ }
48192
+ this.#clearContinuationQuiescence();
48028
48193
  const quiescence = setTimeout(() => {
48029
48194
  this.#continuationQuiescence = null;
48030
- if (this.#active !== active || !active.held || this.#phase !== "open")
48195
+ if (this.#active !== active || !active.held || active.rootSegmentActive || this.#phase !== "open") {
48031
48196
  return;
48197
+ }
48032
48198
  this.#occupancy.releaseContinuations();
48033
48199
  if (this.#occupancy.unsettled)
48034
48200
  return;
@@ -48043,7 +48209,8 @@ var ClaudeHarnessSession = class {
48043
48209
  clearTimeout(this.#continuationQuiescence);
48044
48210
  this.#continuationQuiescence = null;
48045
48211
  }
48046
- #observeRootOutput() {
48212
+ #observeRootOutput(active) {
48213
+ active.rootSegmentActive = true;
48047
48214
  this.#clearContinuationQuiescence();
48048
48215
  }
48049
48216
  #finish(active, outcome) {