aiterm-steer-delivery 0.5.0 → 0.5.2
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 +13 -0
- package/README.md +4 -0
- package/dist/claude-receiver.js +5 -1
- package/dist/hook-main.js +42 -3
- package/dist/process.js +44 -9
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
|
+
## 0.5.2 (2026-11-03)
|
|
2
|
+
|
|
3
|
+
- Preserve safe OS failure details for process reads without exposing command lines or output.
|
|
4
|
+
|
|
1
5
|
# Changelog
|
|
2
6
|
|
|
3
7
|
## Unreleased
|
|
4
8
|
|
|
9
|
+
## 0.5.1
|
|
10
|
+
|
|
11
|
+
Claude Codeのhookが、送信前(PreToolUse)の記録が無い時に送信後(PostToolUse)で例外終了し、channelの待機が張られず返信が次のhookまで止まっていたのを直します。関数の名前・引数・返りの形、hookの登録、置き場の作りは変わりません。hookの置き場に記録ファイルが1つ増えます。
|
|
12
|
+
|
|
13
|
+
- **起きていた事。** 2026-10-31、Macの全試験の負荷中に、Claude Codeの`call_send`のPreToolUse hook(登録timeout 15秒、全processの一覧を取る)が記録(`request.json`)を残さないまま道具が進んだ。PostToolUse hookは`CLAUDE_PARENT_HOOK_UNAVAILABLE`で例外終了し、channelの待機(`runClaudeChannelWaiter`)に進まなかった。その間にchannelへ入った返信2通は、次のhookが走るまで会話へ出なかった。
|
|
14
|
+
- **PostToolUseは、PreToolUseの記録が無ければ「配送の依頼が無い」として0で終え、channelの待機へ進む。** 記録があって壊れている時は従来どおり断る。
|
|
15
|
+
- **hookの実行記録を`<hook root>/hook-events.log`へ1行JSONで残す。** event名・会話ID・tool_use_id・所要ms・終了code・失敗の符号だけで、本文・引数・stderrの文は書かない。1MBを超えたら前の世代を1つ残して入れ替える。記録の失敗でhookの結果は変わらない。
|
|
16
|
+
- **POSIXの先祖の読み(`readProcessLineage`)は、全processの一覧でなく1個ずつ親方向へ最大8段たどる。** 読めない時だけ全一覧へ戻す。返す行の形と`hookOwnerProcess`の判定は同じ。
|
|
17
|
+
|
|
5
18
|
## 0.5.0
|
|
6
19
|
|
|
7
20
|
- Optional native delivery admission passes existing request/parent references through queue, dequeue, emit and wake hooks without creating a second queue or resending input.
|
package/README.md
CHANGED
|
@@ -264,3 +264,7 @@ CI runs the tests on Ubuntu, macOS, and Windows. Pushing a `v<version>` tag publ
|
|
|
264
264
|
## License
|
|
265
265
|
|
|
266
266
|
[MIT](LICENSE)
|
|
267
|
+
|
|
268
|
+
## Architecture
|
|
269
|
+
|
|
270
|
+
[Product boundaries and source index](https://github.com/kitepon/aiterm-steer-delivery/blob/fix/process-probe-details-20261103/docs/architecture/README.md).
|
package/dist/claude-receiver.js
CHANGED
|
@@ -252,8 +252,12 @@ runtime = { running: processIsRunning }) {
|
|
|
252
252
|
export async function runClaudeResultHook(profile, input, emit, root, runtime = {}) {
|
|
253
253
|
const event = z.object({ session_id: z.uuid(), tool_use_id: requestId }).parse(input);
|
|
254
254
|
const parent = { kind: "claude", request_id: event.tool_use_id, session_id: event.session_id, hook_root: root };
|
|
255
|
-
const invocation = readInvocation(profile, parent);
|
|
256
255
|
const dir = directory(parent);
|
|
256
|
+
// PreToolUseの記録が無い(hookがtimeoutし、Claude Codeが道具を先へ進めた時など)。配送の依頼は結べていないので配送無しとして0で終え、
|
|
257
|
+
// 呼ぶ側はchannelの待機へ進む(2026-10-31、Macの負荷中に記録が1回欠け、ここで例外終了して待機が張られず返信が次のhookまで止まった)。
|
|
258
|
+
if (!fs.existsSync(path.join(dir, "request.json")))
|
|
259
|
+
return 0;
|
|
260
|
+
const invocation = readInvocation(profile, parent);
|
|
257
261
|
const binding = path.join(dir, "delivery.json");
|
|
258
262
|
if (!fs.existsSync(binding)) {
|
|
259
263
|
fs.rmSync(dir, { recursive: true });
|
package/dist/hook-main.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// 製品のhook入口から呼ぶ本体。入口ファイルは「import して呼ぶだけ」にし、hook commandのpathは製品が持つ。
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
3
4
|
import { fileURLToPath } from "node:url";
|
|
4
5
|
import { claudeHookRoot, cursorHookRoot } from "./profile.js";
|
|
5
6
|
import { runCodexResultHook } from "./codex-hooks.js";
|
|
@@ -35,16 +36,21 @@ export async function runCodexHookMain(profile, directory = process.argv[2]) {
|
|
|
35
36
|
* 失敗の2を返すと、GrokはStopを「止めずに続ける」、PreToolUseを「拒否」と読む。見分けは、Grokだけが入力へ入れる`hookEventName`。
|
|
36
37
|
*/
|
|
37
38
|
export async function runClaudeHookMain(profile, root = claudeHookRoot(profile)) {
|
|
39
|
+
const started = Date.now();
|
|
40
|
+
let event = null, failure, foreign = false;
|
|
38
41
|
try {
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
event = JSON.parse(await readStdin());
|
|
43
|
+
// Grokの起動では何も記録しない(共有のsettingsのhookが動いただけ)。
|
|
44
|
+
if (event !== null && typeof event === "object" && typeof event.hookEventName === "string") {
|
|
45
|
+
foreign = true;
|
|
41
46
|
return;
|
|
47
|
+
}
|
|
42
48
|
if (profile.delivery_admission && profile.channels)
|
|
43
49
|
throw new DeliveryAdmissionError("DELIVERY_ADMISSION_UNSUPPORTED", "channel入力の実行受付が未接続です");
|
|
44
50
|
const write = (text) => new Promise((resolve, reject) => {
|
|
45
51
|
process.stderr.write(text, error => error ? reject(error) : resolve());
|
|
46
52
|
});
|
|
47
|
-
switch (event
|
|
53
|
+
switch (event?.hook_event_name) {
|
|
48
54
|
case "PreToolUse":
|
|
49
55
|
prepareClaudeHookRequest(event, root);
|
|
50
56
|
break;
|
|
@@ -82,10 +88,43 @@ export async function runClaudeHookMain(profile, root = claudeHookRoot(profile))
|
|
|
82
88
|
}
|
|
83
89
|
}
|
|
84
90
|
catch (error) {
|
|
91
|
+
failure = error;
|
|
85
92
|
process.stderr.write(`${error instanceof Error ? error.message : "CLAUDE_PARENT_HOOK_FAILED"}\n`);
|
|
86
93
|
// PostToolUse exit 2 is a resume signal, not a safe admission refusal.
|
|
87
94
|
process.exitCode = profile.delivery_admission ? 1 : 2;
|
|
88
95
|
}
|
|
96
|
+
finally {
|
|
97
|
+
if (!foreign)
|
|
98
|
+
recordClaudeHookEvent(root, event, started, failure);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* hookの実行記録を`<hook root>/hook-events.log`へ1行JSONで足す。event名・会話ID・tool_use_id・所要・終了code・失敗の符号だけで、
|
|
103
|
+
* 本文・引数・stderrの文は書かない。PreToolUseの記録が欠けた時に、hookが走ったか・timeoutに近かったかを後から確かめる為。
|
|
104
|
+
* 記録の失敗でhookの結果を変えない。
|
|
105
|
+
*/
|
|
106
|
+
function recordClaudeHookEvent(root, event, started, failure) {
|
|
107
|
+
try {
|
|
108
|
+
const input = event !== null && typeof event === "object" ? event : {};
|
|
109
|
+
const safe = (value, pattern) => typeof value === "string" && pattern.test(value) ? value : null;
|
|
110
|
+
const message = failure instanceof Error ? failure.message : failure === undefined ? "" : "CLAUDE_PARENT_HOOK_FAILED";
|
|
111
|
+
const code = failure?.delivery_code;
|
|
112
|
+
fs.mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
113
|
+
const file = path.join(root, "hook-events.log");
|
|
114
|
+
// shortcut: 1MBを超えたら前の世代を1つだけ残して入れ替える, 長い保全が要る時に世代管理へ上げる
|
|
115
|
+
try {
|
|
116
|
+
if (fs.statSync(file).size > 1_000_000)
|
|
117
|
+
fs.renameSync(file, `${file}.1`);
|
|
118
|
+
}
|
|
119
|
+
catch { /* まだ無い */ }
|
|
120
|
+
fs.appendFileSync(file, JSON.stringify({
|
|
121
|
+
at: new Date(started).toISOString(), event: safe(input.hook_event_name, /^[A-Za-z]{1,32}$/u),
|
|
122
|
+
session_id: safe(input.session_id, /^[0-9a-fA-F-]{36}$/u), tool_use_id: safe(input.tool_use_id, /^[A-Za-z0-9_-]{1,160}$/u),
|
|
123
|
+
ms: Date.now() - started, exit: process.exitCode ?? 0,
|
|
124
|
+
code: typeof code === "string" ? code : (failure === undefined ? null : (/^[A-Z][A-Z0-9_]*/u.exec(message)?.[0] ?? "FAILED")),
|
|
125
|
+
}) + "\n", { mode: 0o600 });
|
|
126
|
+
}
|
|
127
|
+
catch { /* 記録の失敗でhookを止めない */ }
|
|
89
128
|
}
|
|
90
129
|
/**
|
|
91
130
|
* Cursorが起動する公式hook。差し込み文をstdoutのJSONへ返す。
|
package/dist/process.js
CHANGED
|
@@ -6,6 +6,15 @@ const isWin = process.platform === "win32";
|
|
|
6
6
|
class ProcessTableError extends Error {
|
|
7
7
|
code = 2;
|
|
8
8
|
}
|
|
9
|
+
function processReadError(probe, failure, result) {
|
|
10
|
+
const code = result.error?.code;
|
|
11
|
+
// Native messages and output can contain argv and credentials.
|
|
12
|
+
const detail = { component: "aiterm-steer-delivery", probe, code: result.error
|
|
13
|
+
? (code && ["ETIMEDOUT", "ENOBUFS", "EAGAIN", "ENOENT", "EACCES", "ENOMEM", "EMFILE", "ENFILE", "E2BIG", "EPERM"].includes(code) ? code : "UNKNOWN") : null,
|
|
14
|
+
status: result.status ?? null, signal: result.signal ?? null,
|
|
15
|
+
stdout_bytes: Buffer.byteLength(result.stdout ?? ""), stderr_bytes: Buffer.byteLength(result.stderr ?? "") };
|
|
16
|
+
return new ProcessTableError(`${failure} ${JSON.stringify(detail)}`);
|
|
17
|
+
}
|
|
9
18
|
export function processIdentity(process) {
|
|
10
19
|
return {
|
|
11
20
|
pid: process.pid, process_group_id: process.process_group_id,
|
|
@@ -63,13 +72,13 @@ export function processExists(pid) {
|
|
|
63
72
|
return error.code === "EPERM";
|
|
64
73
|
}
|
|
65
74
|
}
|
|
66
|
-
function windowsJson(script, failure) {
|
|
75
|
+
function windowsJson(script, failure, probe) {
|
|
67
76
|
const source = ["$ErrorActionPreference='Stop'", "[Console]::OutputEncoding=[Text.UTF8Encoding]::new($false)", script].join("\n");
|
|
68
77
|
const result = spawnSync(resolveWindowsPowerShell7(), ["-NoLogo", "-NoProfile", "-NonInteractive", "-EncodedCommand", Buffer.from(source, "utf16le").toString("base64")], {
|
|
69
78
|
encoding: "utf8", timeout: 15000, maxBuffer: 16 * 1024 * 1024, windowsHide: true,
|
|
70
79
|
});
|
|
71
80
|
if (result.error || result.status !== 0)
|
|
72
|
-
throw
|
|
81
|
+
throw processReadError(probe, failure, result);
|
|
73
82
|
try {
|
|
74
83
|
return JSON.parse(result.stdout);
|
|
75
84
|
}
|
|
@@ -111,7 +120,7 @@ export function readProcessIdentities(pids) {
|
|
|
111
120
|
});
|
|
112
121
|
// 該当するprocessが一つも無い時、psは何も出さずstatus 1で終わる。
|
|
113
122
|
if (result.error || (result.status !== 0 && !(result.status === 1 && !result.stdout.trim()))) {
|
|
114
|
-
throw
|
|
123
|
+
throw processReadError("identity", "OSのprocess一覧を取得できません", result);
|
|
115
124
|
}
|
|
116
125
|
return result.stdout.split("\n").filter(line => line.trim()).map(line => {
|
|
117
126
|
const match = /^\s*(\d+)\s+(\S+\s+\S+\s+\d+\s+\S+\s+\d+)\s*$/.exec(line);
|
|
@@ -130,7 +139,7 @@ export function readProcessIdentities(pids) {
|
|
|
130
139
|
" $rows += [ordered]@{ pid=[int]$id; started_identity=$started }",
|
|
131
140
|
"}",
|
|
132
141
|
"ConvertTo-Json -Compress -InputObject $rows",
|
|
133
|
-
].join("\n"), "Windowsのprocessの開始時刻を取得できません");
|
|
142
|
+
].join("\n"), "Windowsのprocessの開始時刻を取得できません", "identity");
|
|
134
143
|
if (!Array.isArray(rows))
|
|
135
144
|
throw new ProcessTableError("Windows processの読みが配列ではありません");
|
|
136
145
|
return rows.map(row => {
|
|
@@ -153,10 +162,10 @@ export function processIsRunning(pid, startedIdentity) {
|
|
|
153
162
|
* 今までどおり全processの一覧を返す。
|
|
154
163
|
*/
|
|
155
164
|
export function readProcessLineage(pid, depth = 8) {
|
|
156
|
-
if (!isWin)
|
|
157
|
-
return readRuntimeProcesses();
|
|
158
165
|
if (!Number.isSafeInteger(pid) || pid <= 0 || !Number.isSafeInteger(depth) || depth <= 0)
|
|
159
166
|
throw new ProcessTableError("process照会のpidが不正です");
|
|
167
|
+
if (!isWin)
|
|
168
|
+
return readPosixLineage(pid, depth);
|
|
160
169
|
let rows;
|
|
161
170
|
try {
|
|
162
171
|
rows = windowsJson([
|
|
@@ -174,7 +183,7 @@ export function readProcessLineage(pid, depth = 8) {
|
|
|
174
183
|
" $child=$started; $id=$parent",
|
|
175
184
|
"}",
|
|
176
185
|
"ConvertTo-Json -Compress -InputObject $rows",
|
|
177
|
-
].join("\n"), "Windowsのprocessの先祖を取得できません");
|
|
186
|
+
].join("\n"), "Windowsのprocessの先祖を取得できません", "lineage");
|
|
178
187
|
}
|
|
179
188
|
catch {
|
|
180
189
|
return readRuntimeProcesses();
|
|
@@ -192,13 +201,39 @@ export function readProcessLineage(pid, depth = 8) {
|
|
|
192
201
|
};
|
|
193
202
|
});
|
|
194
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* POSIXで先祖を1個ずつたどる。全processの一覧(`ps -axww`、全commandを含む)は混んだMacで秒単位かかり、Claude CodeのPreToolUse
|
|
206
|
+
* hookの15秒に食い込む(2026-10-31)。1個読むpsを最大depth回で済ませ、読めない時だけ全一覧へ戻す。
|
|
207
|
+
*/
|
|
208
|
+
function readPosixLineage(pid, depth) {
|
|
209
|
+
const rows = [];
|
|
210
|
+
for (let id = pid; rows.length < depth && id > 0;) {
|
|
211
|
+
const result = spawnSync("/bin/ps", ["-o", "pid=,ppid=,pgid=,stat=,lstart=,time=,command=", "-p", String(id)], {
|
|
212
|
+
encoding: "utf8", env: { ...process.env, LC_ALL: "C" }, timeout: 10000, maxBuffer: 16 * 1024 * 1024,
|
|
213
|
+
});
|
|
214
|
+
if (result.error || result.status !== 0)
|
|
215
|
+
break;
|
|
216
|
+
let parsed;
|
|
217
|
+
try {
|
|
218
|
+
parsed = parsePosixProcessTable(result.stdout);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
if (parsed.length !== 1 || parsed[0].pid !== id)
|
|
224
|
+
break;
|
|
225
|
+
rows.push(parsed[0]);
|
|
226
|
+
id = parsed[0].parent_pid;
|
|
227
|
+
}
|
|
228
|
+
return rows.length > 0 && rows[0].pid === pid ? rows : readRuntimeProcesses();
|
|
229
|
+
}
|
|
195
230
|
export function readRuntimeProcesses() {
|
|
196
231
|
if (!isWin) {
|
|
197
232
|
const result = spawnSync("/bin/ps", ["-axww", "-o", "pid=,ppid=,pgid=,stat=,lstart=,time=,command="], {
|
|
198
233
|
encoding: "utf8", env: { ...process.env, LC_ALL: "C" }, timeout: 10000, maxBuffer: 16 * 1024 * 1024,
|
|
199
234
|
});
|
|
200
235
|
if (result.error || result.status !== 0)
|
|
201
|
-
throw
|
|
236
|
+
throw processReadError("table", "OSのprocess一覧を取得できません", result);
|
|
202
237
|
return parsePosixProcessTable(result.stdout);
|
|
203
238
|
}
|
|
204
239
|
const script = [
|
|
@@ -212,7 +247,7 @@ export function readRuntimeProcesses() {
|
|
|
212
247
|
encoding: "utf8", timeout: 15000, maxBuffer: 16 * 1024 * 1024, windowsHide: true,
|
|
213
248
|
});
|
|
214
249
|
if (result.error || result.status !== 0)
|
|
215
|
-
throw
|
|
250
|
+
throw processReadError("table", "Windowsのnative process一覧を取得できません", result);
|
|
216
251
|
let rows;
|
|
217
252
|
try {
|
|
218
253
|
rows = JSON.parse(result.stdout);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aiterm-steer-delivery",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Aiterm's steer delivery as a library: deliver text to the parent AI session that called your MCP tool — steered into the running turn when it is busy, delivered normally when it is idle. Codex (official queue + sync hooks), Claude Code (asyncRewake hooks), Cursor (hooks + background receiver).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|