@coda-console/claude 1.14.0 → 1.14.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/README.md CHANGED
@@ -20,6 +20,16 @@ Coda 给会话一个临时目录时(环境变量 `CODA_SESSION_TMP`,绝对
20
20
  - 已经留在 `/private/tmp/claude-*` 下的存量目录不在这里处理。
21
21
  - 上一节的 `coda-claude-mcp-*` 会话结束时由入口自己删,不在这个目录里。
22
22
 
23
+ ## 会话怎样结束、结束后怎么答
24
+
25
+ SDK 的查询自己结束(Claude 进程被杀、崩溃、正常退出)时,入口把原因记下来:查询抛的错误文字(SDK 已把原生进程的退出码或信号和 stderr 末尾几行写在里面),没有错误就写「Claude SDK 查询结束,没有给出错误」。
26
+ 原因写进插件的 stderr 日志(`Claude 会话结束:…`),并放进还在进行的那一轮的错误信息(`Claude 会话已结束:…`)。Coda 主动关闭的会话不写日志。
27
+
28
+ 这些错误都标 `session_ended`(原生会话或进程已结束,不是上游服务的问题,Coda 不按服务故障重试)。`input` 只写插件能确定的:正在跑的那一轮的输入是 `accepted`,排在它后面没跑过的是 `not_accepted`,没有一轮在跑时输入有没有被读走说不准,不写。
29
+
30
+ 会话已结束后再来的 `session/prompt` 当场拒绝,错误文字是 `Claude 会话未在运行:<原因>`,标 `session_ended`、`input: "not_accepted"`(输入没有进原生会话,Coda 整条重发)。
31
+ 换模型服务时重启查询失败、输入还没交给新查询的,同样是 `session_ended` + `not_accepted`;失败本身已带别的类别(如额度、服务)的保留原类别,类别允许 `input` 时补上 `not_accepted`(`auth_required` 不带 `input`,原样保留)。设置项请求(`session/set_config_option`)只带原因,不带标记。
32
+
23
33
  ## 失败怎样分类
24
34
 
25
35
  一轮失败时,入口(`agent/errors.mjs` 的 `claudeFailure`,纯函数)只按最终错误标失败类别(协议 `failure.json`),重试策略归 Coda,插件里没有重试或等待。
package/agent/session.mjs CHANGED
@@ -51,6 +51,16 @@ const takenIn = m => m.user_message_uuids ?? (m.user_message_uuid ? [m.user_mess
51
51
  const conversing = m => !m.parent_tool_use_id && (['assistant', 'stream_event'].includes(m.type)
52
52
  || (m.type === 'user' && (!!m.origin || (Array.isArray(m.message?.content) && m.message.content.some(block => block?.type === 'tool_result')))));
53
53
  const failure = (message, code = -32603) => new RequestError(code, message);
54
+ // A request for a session not running, with why (what ended it, `endReason`).
55
+ const notRunning = reason => failure(`Claude 会话未在运行${reason ? `:${reason}` : ''}`, -32001);
56
+ // An input refused before it went into the native session: nothing ran it, so Coda may send it again (docs/agent-protocol.md
57
+ // 「原生会话已结束」: `session_ended`, `not_accepted`). A failure the error already names (a quota, a service) keeps its kind,
58
+ // with `not_accepted` where the kind takes an input (not `auth_required`).
59
+ const refused = error => {
60
+ const own = error.data?._meta?.coda?.failure;
61
+ const failure = !own ? { kind: 'session_ended', input: 'not_accepted' } : own.kind === 'auth_required' ? own : { ...own, input: 'not_accepted' };
62
+ return new RequestError(error.code ?? -32603, error.message, { _meta: { coda: { failure } } });
63
+ };
54
64
  const denied = { behavior: 'deny', message: 'Coda 没有在请求结束前给出审批' };
55
65
  // A child session's output that comes before its task, kept until the task names that session: all of it, since what streamed
56
66
  // counts as said, a message's streamed pieces as one.
@@ -246,7 +256,7 @@ export class ClaudeSession {
246
256
  this.query = null; this.input.close();
247
257
  try { previous.close(); } catch {}
248
258
  await this.pump;
249
- if (this.phase !== 'running') throw failure('Claude 会话未在运行', -32001);
259
+ if (this.phase !== 'running') throw notRunning(this.endReason);
250
260
  // One that cannot be started, or does not start, ends the session, as one that ends of itself does: no later turn waits on it.
251
261
  try {
252
262
  const said = await this.sdk.getSessionInfo(this.sessionId, { dir: this.cwd });
@@ -270,7 +280,7 @@ export class ClaudeSession {
270
280
  // Whether the next turn runs in another query than this one: on another service's model, or back on Claude's own.
271
281
  rerouted() { return (this.served(this.current.model)?.id ?? null) !== this.serviceModel; }
272
282
  async configure(configId, value) {
273
- if (this.phase !== 'running') throw failure('Claude 会话未在运行', -32001);
283
+ if (this.phase !== 'running') throw notRunning(this.endReason);
274
284
  if (configId === 'mode') {
275
285
  if (!MODES.some(mode => mode.id === value)) throw failure('审批模式不可用', -32602);
276
286
  await this.query.setPermissionMode(value); this.current.mode = value;
@@ -298,10 +308,11 @@ export class ClaudeSession {
298
308
  }
299
309
  // Answered when the native result that names this input arrives; accepting it into the input says nothing yet.
300
310
  async prompt({ prompt }) {
301
- if (this.phase !== 'running') throw failure('Claude 会话未在运行', -32001);
311
+ if (this.phase !== 'running') throw refused(notRunning(this.endReason));
302
312
  if (this.rerouted()) {
303
313
  if (!this.rerouting) { this.cancelling = false; this.rerouting = this.reroute().finally(() => { this.rerouting = null; }); }
304
- await this.rerouting;
314
+ // A query that cannot be moved to fails here, before the input is pushed.
315
+ try { await this.rerouting; } catch (error) { throw refused(error); }
305
316
  // Stopped while it moved: the input is not given to the new query.
306
317
  if (this.cancelling) { this.cancelling = false; return { stopReason: 'cancelled' }; }
307
318
  }
@@ -445,10 +456,24 @@ export class ClaudeSession {
445
456
  end(query, error) {
446
457
  if (this.phase === 'ended') return;
447
458
  const intended = this.phase === 'closing';
459
+ // What ended it, kept for the turns that go on asking and written to the log: a query that ends without an error says so.
460
+ this.endReason = intended ? 'Coda 关闭了会话' : error?.message || 'Claude SDK 查询结束,没有给出错误';
461
+ if (!intended) process.stderr.write(`Claude 会话结束:${this.endReason}\n`);
462
+ const held = this.held, compacting = this.compacting;
448
463
  this.phase = 'ended'; this.input.close(); this.steering.clear(); this.held = null; this.ending = null;
449
464
  this.compacting?.answer({ compacted: false, reason: error?.message ?? 'Claude 会话已结束' }); this.compacting = null;
450
465
  for (const withdrawn of this.withdrawals) withdrawn.abort();
451
- for (const { reject } of this.prompts.values()) reject(failure(error?.message ?? 'Claude 会话已结束'));
466
+ // Each prompt says whether the native session had its input, where that is known: the one a native turn of Coda's is running
467
+ // (or whose turn ended with messages left to take) was taken, those queued behind it never ran. With no such turn going, an
468
+ // input may or may not have been read before the session ended: not said. A compaction Coda asked for came before any prompt
469
+ // that waits (compact), and the turn going is its: those prompts have not run.
470
+ const running = !compacting && this.turning && !this.opening && !this.waits.handling && !this.waits.unclaimed;
471
+ let first = true;
472
+ for (const [id, { reject }] of this.prompts) {
473
+ const input = id === held || running && first ? 'accepted' : running || held || compacting ? 'not_accepted' : undefined;
474
+ first = false;
475
+ reject(new RequestError(-32603, `Claude 会话已结束:${this.endReason}`, { _meta: { coda: { failure: { kind: 'session_ended', ...(input && { input }) } } } }));
476
+ }
452
477
  this.prompts.clear();
453
478
  try { query?.close(); } catch {}
454
479
  this.dropMcpConfig();
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "claude",
4
4
  "displayName": "Claude",
5
5
  "description": "用 Claude Agent SDK 跑会话",
6
- "version": "1.14.0",
6
+ "version": "1.14.1",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],