@coda-console/claude 1.13.2 → 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 +22 -0
- package/agent/session.mjs +33 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,28 @@ Coda 交来的 MCP 服务(含 `coda` 服务 `env` 里的令牌 `CODA_TOKEN`)
|
|
|
8
8
|
入口改为:会话打开时在系统临时目录下 `mkdtemp` 建目录(0700),把 `{ mcpServers }` 写进里面的 `mcp.json`(0600),用 `extraArgs` 交 `--mcp-config <路径>`。
|
|
9
9
|
入口这一侧,令牌不再进命令行,只写进这个文件;会话结束(进程被关、查询自己结束)时连目录一起删。进程崩溃留下的目录不另外清理:令牌随托管记录作废,系统临时目录也会自己回收。没有 MCP 服务的会话不建目录。
|
|
10
10
|
|
|
11
|
+
## 临时目录
|
|
12
|
+
|
|
13
|
+
Claude Code 自己会在系统临时目录下建 `claude-<uid>/<cwd 编码>/<原生会话号>/`(里面是 `scratchpad`、`tasks`),它没有回收,会话多了会写满磁盘。
|
|
14
|
+
Coda 给会话一个临时目录时(环境变量 `CODA_SESSION_TMP`,绝对路径),入口把它交给 Claude Code 的环境变量 `CLAUDE_CODE_TMPDIR`,上面的目录就建在
|
|
15
|
+
`<CODA_SESSION_TMP>/claude-<uid>/…` 下,系统提示里告诉模型的 scratchpad 路径也跟着变;`resume` 恢复的原生会话照样生效,选了模型服务而另起的查询也一样。
|
|
16
|
+
实地核对过(SDK 0.3.322):新会话与 `resume` 的 scratchpad 都在给定目录下。
|
|
17
|
+
|
|
18
|
+
- **什么时候没有:** `CODA_SESSION_TMP` 没有值时不设 `CLAUDE_CODE_TMPDIR`,和以前一样放在系统临时目录。旧的后端(含已发布的正式版)和没有状态根的后端不给这个变量。
|
|
19
|
+
- **谁回收:** Coda。它在启动 Agent 之前建好这个目录,会话归档、宽限期到了、进程关掉之后连同里面的东西一起删;插件不删,也不监听归档。
|
|
20
|
+
- 已经留在 `/private/tmp/claude-*` 下的存量目录不在这里处理。
|
|
21
|
+
- 上一节的 `coda-claude-mcp-*` 会话结束时由入口自己删,不在这个目录里。
|
|
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
|
+
|
|
11
33
|
## 失败怎样分类
|
|
12
34
|
|
|
13
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.
|
|
@@ -167,7 +177,9 @@ export class ClaudeSession {
|
|
|
167
177
|
...(Object.keys(servers).length ? { extraArgs: { 'mcp-config': this.mcpConfig({ mcpServers: servers }) } } : {}), ...(additionalDirectories.length ? { additionalDirectories } : {}),
|
|
168
178
|
// Identify Coda even when a parent supplied an SDK label (anthropics/claude-code#97074).
|
|
169
179
|
// Claude Code reports running and idle only when asked: idle is what ends a task's wait.
|
|
170
|
-
|
|
180
|
+
// Claude Code's own temp root (scratchpad, tasks) goes under the directory Coda reclaims with the session; older backends give none.
|
|
181
|
+
env: { ...environment, CLAUDE_CODE_ENTRYPOINT: 'coda', CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS: '1',
|
|
182
|
+
...(this.env.CODA_SESSION_TMP ? { CLAUDE_CODE_TMPDIR: this.env.CODA_SESSION_TMP } : {}) },
|
|
171
183
|
// Stop supplies the complete list of scheduled wakeups. A CronCreate result alone cannot tell whether one was later
|
|
172
184
|
// deleted or fired.
|
|
173
185
|
hooks: { Stop: [{ hooks: [async input => {
|
|
@@ -244,7 +256,7 @@ export class ClaudeSession {
|
|
|
244
256
|
this.query = null; this.input.close();
|
|
245
257
|
try { previous.close(); } catch {}
|
|
246
258
|
await this.pump;
|
|
247
|
-
if (this.phase !== 'running') throw
|
|
259
|
+
if (this.phase !== 'running') throw notRunning(this.endReason);
|
|
248
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.
|
|
249
261
|
try {
|
|
250
262
|
const said = await this.sdk.getSessionInfo(this.sessionId, { dir: this.cwd });
|
|
@@ -268,7 +280,7 @@ export class ClaudeSession {
|
|
|
268
280
|
// Whether the next turn runs in another query than this one: on another service's model, or back on Claude's own.
|
|
269
281
|
rerouted() { return (this.served(this.current.model)?.id ?? null) !== this.serviceModel; }
|
|
270
282
|
async configure(configId, value) {
|
|
271
|
-
if (this.phase !== 'running') throw
|
|
283
|
+
if (this.phase !== 'running') throw notRunning(this.endReason);
|
|
272
284
|
if (configId === 'mode') {
|
|
273
285
|
if (!MODES.some(mode => mode.id === value)) throw failure('审批模式不可用', -32602);
|
|
274
286
|
await this.query.setPermissionMode(value); this.current.mode = value;
|
|
@@ -296,10 +308,11 @@ export class ClaudeSession {
|
|
|
296
308
|
}
|
|
297
309
|
// Answered when the native result that names this input arrives; accepting it into the input says nothing yet.
|
|
298
310
|
async prompt({ prompt }) {
|
|
299
|
-
if (this.phase !== 'running') throw
|
|
311
|
+
if (this.phase !== 'running') throw refused(notRunning(this.endReason));
|
|
300
312
|
if (this.rerouted()) {
|
|
301
313
|
if (!this.rerouting) { this.cancelling = false; this.rerouting = this.reroute().finally(() => { this.rerouting = null; }); }
|
|
302
|
-
|
|
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); }
|
|
303
316
|
// Stopped while it moved: the input is not given to the new query.
|
|
304
317
|
if (this.cancelling) { this.cancelling = false; return { stopReason: 'cancelled' }; }
|
|
305
318
|
}
|
|
@@ -443,10 +456,24 @@ export class ClaudeSession {
|
|
|
443
456
|
end(query, error) {
|
|
444
457
|
if (this.phase === 'ended') return;
|
|
445
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;
|
|
446
463
|
this.phase = 'ended'; this.input.close(); this.steering.clear(); this.held = null; this.ending = null;
|
|
447
464
|
this.compacting?.answer({ compacted: false, reason: error?.message ?? 'Claude 会话已结束' }); this.compacting = null;
|
|
448
465
|
for (const withdrawn of this.withdrawals) withdrawn.abort();
|
|
449
|
-
|
|
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
|
+
}
|
|
450
477
|
this.prompts.clear();
|
|
451
478
|
try { query?.close(); } catch {}
|
|
452
479
|
this.dropMcpConfig();
|