aiterm-steer-delivery 0.5.0 → 0.5.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 +9 -0
- package/dist/claude-receiver.js +5 -1
- package/dist/hook-main.js +42 -3
- package/dist/process.js +28 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.5.1
|
|
6
|
+
|
|
7
|
+
Claude Codeのhookが、送信前(PreToolUse)の記録が無い時に送信後(PostToolUse)で例外終了し、channelの待機が張られず返信が次のhookまで止まっていたのを直します。関数の名前・引数・返りの形、hookの登録、置き場の作りは変わりません。hookの置き場に記録ファイルが1つ増えます。
|
|
8
|
+
|
|
9
|
+
- **起きていた事。** 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が走るまで会話へ出なかった。
|
|
10
|
+
- **PostToolUseは、PreToolUseの記録が無ければ「配送の依頼が無い」として0で終え、channelの待機へ進む。** 記録があって壊れている時は従来どおり断る。
|
|
11
|
+
- **hookの実行記録を`<hook root>/hook-events.log`へ1行JSONで残す。** event名・会話ID・tool_use_id・所要ms・終了code・失敗の符号だけで、本文・引数・stderrの文は書かない。1MBを超えたら前の世代を1つ残して入れ替える。記録の失敗でhookの結果は変わらない。
|
|
12
|
+
- **POSIXの先祖の読み(`readProcessLineage`)は、全processの一覧でなく1個ずつ親方向へ最大8段たどる。** 読めない時だけ全一覧へ戻す。返す行の形と`hookOwnerProcess`の判定は同じ。
|
|
13
|
+
|
|
5
14
|
## 0.5.0
|
|
6
15
|
|
|
7
16
|
- 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/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
|
@@ -153,10 +153,10 @@ export function processIsRunning(pid, startedIdentity) {
|
|
|
153
153
|
* 今までどおり全processの一覧を返す。
|
|
154
154
|
*/
|
|
155
155
|
export function readProcessLineage(pid, depth = 8) {
|
|
156
|
-
if (!isWin)
|
|
157
|
-
return readRuntimeProcesses();
|
|
158
156
|
if (!Number.isSafeInteger(pid) || pid <= 0 || !Number.isSafeInteger(depth) || depth <= 0)
|
|
159
157
|
throw new ProcessTableError("process照会のpidが不正です");
|
|
158
|
+
if (!isWin)
|
|
159
|
+
return readPosixLineage(pid, depth);
|
|
160
160
|
let rows;
|
|
161
161
|
try {
|
|
162
162
|
rows = windowsJson([
|
|
@@ -192,6 +192,32 @@ export function readProcessLineage(pid, depth = 8) {
|
|
|
192
192
|
};
|
|
193
193
|
});
|
|
194
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* POSIXで先祖を1個ずつたどる。全processの一覧(`ps -axww`、全commandを含む)は混んだMacで秒単位かかり、Claude CodeのPreToolUse
|
|
197
|
+
* hookの15秒に食い込む(2026-10-31)。1個読むpsを最大depth回で済ませ、読めない時だけ全一覧へ戻す。
|
|
198
|
+
*/
|
|
199
|
+
function readPosixLineage(pid, depth) {
|
|
200
|
+
const rows = [];
|
|
201
|
+
for (let id = pid; rows.length < depth && id > 0;) {
|
|
202
|
+
const result = spawnSync("/bin/ps", ["-o", "pid=,ppid=,pgid=,stat=,lstart=,time=,command=", "-p", String(id)], {
|
|
203
|
+
encoding: "utf8", env: { ...process.env, LC_ALL: "C" }, timeout: 10000, maxBuffer: 16 * 1024 * 1024,
|
|
204
|
+
});
|
|
205
|
+
if (result.error || result.status !== 0)
|
|
206
|
+
break;
|
|
207
|
+
let parsed;
|
|
208
|
+
try {
|
|
209
|
+
parsed = parsePosixProcessTable(result.stdout);
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
if (parsed.length !== 1 || parsed[0].pid !== id)
|
|
215
|
+
break;
|
|
216
|
+
rows.push(parsed[0]);
|
|
217
|
+
id = parsed[0].parent_pid;
|
|
218
|
+
}
|
|
219
|
+
return rows.length > 0 && rows[0].pid === pid ? rows : readRuntimeProcesses();
|
|
220
|
+
}
|
|
195
221
|
export function readRuntimeProcesses() {
|
|
196
222
|
if (!isWin) {
|
|
197
223
|
const result = spawnSync("/bin/ps", ["-axww", "-o", "pid=,ppid=,pgid=,stat=,lstart=,time=,command="], {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aiterm-steer-delivery",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
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",
|