@nathapp/nax-agent-acp 0.91.5 → 0.91.7-canary.1

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/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to `@nathapp/nax-agent-acp` are recorded here. Versions move in
4
4
  step with `@nathapp/nax-agent`.
5
5
 
6
+ ## [0.91.7-canary.1] - 2026-10-24
7
+
8
+ - No changes in this package. Released in lockstep with the other nax packages.
9
+
10
+ ## [0.91.6] - 2026-10-24
11
+
12
+ - No changes in this package. Released in lockstep with the other nax packages.
13
+
6
14
  ## [0.91.5] - 2026-10-23
7
15
 
8
16
  - No changes in this package. Released in lockstep with the other nax packages.
@@ -161,7 +161,11 @@ export function createSessionRegistry(deps) {
161
161
  await entry.server.switchTo(() => target(sessionId, entry.meta.cwd, next, entry.mcp.embedderTools(next.mode)), () => target(sessionId, entry.meta.cwd, from, entry.mcp.embedderTools(from.mode)));
162
162
  }
163
163
  catch (error) {
164
- if (entry.server.isClosed()) {
164
+ // A terminal close that interrupted the switch owns the cleanup (Approach 4):
165
+ // its closeRegistryEntry/closeEntries/delete awaits close(), then releases the
166
+ // lock and closes the MCP tools. Cleaning up here too would release the lock
167
+ // twice. Only a switch that failed on its own leaves a dead entry to clean.
168
+ if (entry.server.isClosed() && !entry.server.isClosing()) {
165
169
  try {
166
170
  await closeEntry(sessionId, entry);
167
171
  }
@@ -41,6 +41,8 @@ export interface ServerSession {
41
41
  /** Close-and-reopen for a mode or config change (spec §3.3); `restore` reopens the old settings when `open` fails. */
42
42
  switchTo(open: () => Promise<SwitchTarget>, restore: () => Promise<SwitchTarget>): Promise<void>;
43
43
  isClosed(): boolean;
44
+ /** A terminal close() was requested, whether or not it has finished (spec §Design 1). */
45
+ isClosing(): boolean;
44
46
  cancel(): void;
45
47
  close(): Promise<void>;
46
48
  }
@@ -21,6 +21,10 @@ const CANCELLED_BEFORE_SEND = {
21
21
  response: { stopReason: "cancelled", usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 } },
22
22
  notices: [],
23
23
  };
24
+ const SESSION_CLOSED = "session is closed";
25
+ function sessionClosed() {
26
+ return RequestError.invalidRequest(undefined, SESSION_CLOSED);
27
+ }
24
28
  function react(event, turn, translator) {
25
29
  switch (event.type) {
26
30
  case "approval_requested":
@@ -78,6 +82,12 @@ export function createServerSession(deps) {
78
82
  let queued = [];
79
83
  let costUsd = deps.priorCostUsd ?? 0;
80
84
  let closed = false;
85
+ /** The closing flag: set by the first close(), so isClosing() is true from then on. */
86
+ let closeRequested = false;
87
+ /** The in-flight switchTo, while its gap runs (§Design 2). */
88
+ let switching;
89
+ /** Whether the current agent is a live S3 session: false from the moment a switch closes it. */
90
+ let live = true;
81
91
  let running = false;
82
92
  /** A cancel before send() claimed the S3 turn, where AgentSession.cancel() is a no-op. */
83
93
  let cancelBeforeSend = false;
@@ -150,9 +160,68 @@ export function createServerSession(deps) {
150
160
  }
151
161
  return outcome;
152
162
  }
163
+ /** Adopts the session a switch's `open` or `restore` returned, and marks it live. */
164
+ function adopt(target) {
165
+ agent = target.session;
166
+ contextWindow = target.contextWindow;
167
+ live = true;
168
+ }
169
+ /** `open`, else `restore`; the open error survives even a successful restore. */
170
+ async function openOrRestore(open, restore) {
171
+ try {
172
+ return { kind: "opened", target: await open() };
173
+ }
174
+ catch (failure) {
175
+ try {
176
+ return { kind: "restored", target: await restore(), failure };
177
+ }
178
+ catch (restoreError) {
179
+ deps.logger.error("session", "could not reopen the session after a failed switch", {
180
+ sessionId: deps.session.id,
181
+ error: messageOf(restoreError),
182
+ });
183
+ return { kind: "failed", failure };
184
+ }
185
+ }
186
+ }
187
+ /**
188
+ * One switchTo (§3.3, §Design 2-3): close the old S3 session, then adopt what
189
+ * `open` or `restore` left. A close requested meanwhile keeps the session
190
+ * closed and turns the outcome into `session is closed`, after the adoption so
191
+ * close() still knows what to close. Any failed open rejects, whatever its
192
+ * reason, including an `undefined` one.
193
+ */
194
+ async function switchSession(open, restore) {
195
+ stopWaiting();
196
+ closed = true;
197
+ live = false;
198
+ try {
199
+ await agent.close();
200
+ }
201
+ catch (error) {
202
+ deps.logger.warn("session", "session close failed during switch", {
203
+ sessionId: deps.session.id,
204
+ error: messageOf(error),
205
+ });
206
+ }
207
+ const outcome = await openOrRestore(open, restore);
208
+ // Nothing was adopted: the session stays dead for the registry's cleanup.
209
+ if (outcome.kind === "failed") {
210
+ if (closeRequested)
211
+ throw sessionClosed();
212
+ throw outcome.failure;
213
+ }
214
+ adopt(outcome.target);
215
+ if (closeRequested)
216
+ throw sessionClosed();
217
+ closed = false;
218
+ if (outcome.kind === "restored")
219
+ throw outcome.failure;
220
+ }
153
221
  return {
154
222
  id: deps.session.id,
155
223
  isClosed: () => closed,
224
+ isClosing: () => closeRequested,
156
225
  get running() {
157
226
  return running;
158
227
  },
@@ -163,7 +232,7 @@ export function createServerSession(deps) {
163
232
  if (running)
164
233
  throw turnInProgress();
165
234
  if (closed)
166
- throw RequestError.invalidRequest(undefined, "session is closed");
235
+ throw sessionClosed();
167
236
  const message = flattenPrompt(blocks);
168
237
  running = true;
169
238
  cancelBeforeSend = false;
@@ -194,45 +263,18 @@ export function createServerSession(deps) {
194
263
  },
195
264
  async switchTo(open, restore) {
196
265
  if (closed)
197
- throw RequestError.invalidRequest(undefined, "session is closed");
266
+ throw sessionClosed();
198
267
  if (running)
199
268
  throw turnInProgress();
200
269
  running = true;
270
+ const work = switchSession(open, restore);
271
+ switching = work;
201
272
  try {
202
- stopWaiting();
203
- closed = true;
204
- try {
205
- await agent.close();
206
- }
207
- catch (error) {
208
- deps.logger.warn("session", "session close failed during switch", {
209
- sessionId: deps.session.id,
210
- error: messageOf(error),
211
- });
212
- }
213
- try {
214
- const next = await open();
215
- closed = false;
216
- agent = next.session;
217
- contextWindow = next.contextWindow;
218
- }
219
- catch (error) {
220
- try {
221
- const back = await restore();
222
- closed = false;
223
- agent = back.session;
224
- contextWindow = back.contextWindow;
225
- }
226
- catch (restoreError) {
227
- deps.logger.error("session", "could not reopen the session after a failed switch", {
228
- sessionId: deps.session.id,
229
- error: messageOf(restoreError),
230
- });
231
- }
232
- throw error;
233
- }
273
+ await work;
234
274
  }
235
275
  finally {
276
+ // Cleared before this promise settles, whatever the outcome (§Design 2).
277
+ switching = undefined;
236
278
  running = false;
237
279
  }
238
280
  },
@@ -245,15 +287,26 @@ export function createServerSession(deps) {
245
287
  close() {
246
288
  if (closing !== undefined)
247
289
  return closing;
248
- if (closed)
249
- return Promise.resolve();
290
+ closeRequested = true;
291
+ // A switch whose open and restore both failed leaves a dead session that
292
+ // the registry's dead-entry cleanup closes: nothing here to close.
293
+ if (closed && switching === undefined) {
294
+ closing = Promise.resolve();
295
+ return closing;
296
+ }
297
+ const inFlight = switching;
250
298
  closed = true; // Refuse new prompts/switches before the first await.
251
299
  cancelBeforeSend = true;
252
300
  agent.cancel("session is closing");
253
301
  stopWaiting();
254
302
  closing = (async () => {
303
+ // The switch owns `agent` until it settles; then close only what it
304
+ // left live, so a switch that left nothing is not closed twice.
305
+ if (inFlight !== undefined)
306
+ await inFlight.catch(() => undefined);
255
307
  try {
256
- await agent.close();
308
+ if (live)
309
+ await agent.close();
257
310
  }
258
311
  finally {
259
312
  // AgentSession.close need not await the facade's event delivery or
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nathapp/nax-agent-acp",
3
- "version": "0.91.5",
3
+ "version": "0.91.7-canary.1",
4
4
  "description": "ACP (Agent Client Protocol) backend for @nathapp/nax-agent sessions: drive Claude Code and other ACP agents through the nax-agent session API.",
5
5
  "license": "MIT",
6
6
  "author": "William Khoo",
@@ -49,12 +49,12 @@
49
49
  "zod": "^4.3.6"
50
50
  },
51
51
  "peerDependencies": {
52
- "@nathapp/nax-agent": "^0.91.5"
52
+ "@nathapp/nax-agent": "^0.91.7-canary.1"
53
53
  },
54
54
  "publishConfig": {
55
55
  "access": "public",
56
56
  "registry": "https://registry.npmjs.org/",
57
57
  "provenance": true,
58
- "tag": "latest"
58
+ "tag": "canary"
59
59
  }
60
60
  }