aiterm-steer-delivery 0.2.1 → 0.2.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 +9 -0
- package/dist/claude-receiver.d.ts +7 -0
- package/dist/claude-receiver.js +79 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.2
|
|
4
|
+
|
|
5
|
+
Claude Codeの親への単発配送で、依頼ごとの置き場(`<state_root>/claude-parent-hooks/<tool_use_id>/`)が届いた後も残っていたのを片付けます。APIと記録の形は変わりません。
|
|
6
|
+
|
|
7
|
+
- **届いた依頼の置き場を、送り手が消す。** `submitClaudeParentAnswer`は、受信hookが本文を出し終えた記録(`emitted.json`)を確かめた後に、置き場ごと消す。今までは届いた子1つにつき置き場が1つ残り、Windowsの一時置き場では再起動でも消えなかった(18日前の分が残っていた)。消せなかった時も配送は成功のまま返す。届かなかった時(誤りで返す時)は消さず、原因を調べる材料として残す。
|
|
8
|
+
- **届かないまま残った置き場を、PreToolUseのhookが見回って消す。** 誤りで返った呼び出しや打ち切られた呼び出しはPostToolUseが走らず、`request.json`だけの置き場が残っていた。消すのは、最後に書かれてから1日たち、待っている配送が無い置き場(配送を結んでいない、依頼元のClaude processが居ない、出し終えている、依頼元を読めない)。依頼元が居て配送を結んであり、まだ出し終えていない置き場は、何日たっても残す。この製品の依頼の記録でないfileが入ったdirectoryには触れない。process表は、hookが親を記録するために読んだものを使い、読む回数は増えない。
|
|
9
|
+
- **会話終了の見回り(`closeClaudeParentSession`)が、読めない`request.json`で止まらない。** 今までは1つでも読めないと例外で終わり、その会話の残りの依頼を閉じなかった。読めない記録と、消えている途中の置き場は飛ばす。
|
|
10
|
+
- 依頼元のClaude processが居なくなってから1日より後に子が終わった配送は、置き場が消えているので、`CLAUDE_PARENT_PROCESS_CLOSED`ではなく`CLAUDE_PARENT_HOOK_UNAVAILABLE`で返る(どちらも届けていない。OSの再起動で一時置き場が消えた後と同じ)。
|
|
11
|
+
|
|
3
12
|
## 0.2.1
|
|
4
13
|
|
|
5
14
|
- `withCodexReceiver`の`runtime`に`env`を足す。Codex App Serverを起こす環境を渡せる(省略すると、今までどおりこのprocessの環境)。`CODEX_HOME`は常に`parent.codex_home`で上書きする。Aitermが、認証sessionへ引き継いだ環境のままCodexのログインの状態を公式App Serverへ聞くために使う。
|
|
@@ -9,6 +9,13 @@ export declare const claudeParentSchema: z.ZodObject<{
|
|
|
9
9
|
}, z.core.$strict>;
|
|
10
10
|
export type ClaudeParent = z.infer<typeof claudeParentSchema>;
|
|
11
11
|
export declare function prepareClaudeHookRequest(input: unknown, root: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* 届かないまま残った依頼の置き場を消し、消した数を返す。誤りで返った呼び出しや打ち切られた呼び出しはPostToolUseが走らず、
|
|
14
|
+
* 置き場が残る。Windowsの一時置き場は再起動でも消えない。
|
|
15
|
+
* 消すのは、最後に書かれてから1日たち、待っている配送が無い置き場だけ。依頼元のClaude processが居て、配送を結んであり、
|
|
16
|
+
* まだ出し終えていない置き場は、何日たっても残す(子が長く動いている配送)。1日は、届かなかった時に原因を調べる間。
|
|
17
|
+
*/
|
|
18
|
+
export declare function sweepClaudeHookRequests(root: string, rows: readonly RuntimeProcess[], now?: number): number;
|
|
12
19
|
/** 起動時envのsession IDは/clearで古くなるため、実際のPreToolUseとの相関だけを使う。 */
|
|
13
20
|
export declare function claudeParentFromRequest(profile: ProductProfile, clientName: string | undefined, metadata: unknown, root: string): ClaudeParent | null;
|
|
14
21
|
export declare function verifyClaudeParent(profile: ProductProfile, parent: ClaudeParent): void;
|
package/dist/claude-receiver.js
CHANGED
|
@@ -38,7 +38,8 @@ function assertOpen(parent) {
|
|
|
38
38
|
export function prepareClaudeHookRequest(input, root) {
|
|
39
39
|
const event = z.object({ session_id: z.uuid(), tool_use_id: requestId, agent_id: z.string().optional() }).parse(input);
|
|
40
40
|
// shellを通す形のhookでは、直接の親はshellになる。shellはこのhookと一緒に終わるので、その先のClaude processを記録する。
|
|
41
|
-
const
|
|
41
|
+
const rows = readRuntimeProcesses();
|
|
42
|
+
const owner = hookOwnerProcess(rows, process.ppid);
|
|
42
43
|
if (!owner)
|
|
43
44
|
throw new ClaudeDeliveryError("CLAUDE_PARENT_PROCESS_UNAVAILABLE", "hookを起動した親processを確認できません");
|
|
44
45
|
const dir = path.join(root, event.tool_use_id);
|
|
@@ -47,6 +48,59 @@ export function prepareClaudeHookRequest(input, root) {
|
|
|
47
48
|
request_id: event.tool_use_id, session_id: event.session_id, agent_id: event.agent_id ?? null,
|
|
48
49
|
parent_pid: owner.pid, parent_started_identity: owner.started_identity,
|
|
49
50
|
});
|
|
51
|
+
// 記録を置いた後に見回る。process表は上で読んだものを使う。見回りの失敗で依頼を止めない。
|
|
52
|
+
try {
|
|
53
|
+
sweepClaudeHookRequests(root, rows);
|
|
54
|
+
}
|
|
55
|
+
catch { /* 次の依頼でまた見回る */ }
|
|
56
|
+
}
|
|
57
|
+
// 届いた依頼の置き場は送り手が消す。見回りが消すのは、届かないまま残った分。
|
|
58
|
+
const STALE_REQUEST_MS = 24 * 60 * 60 * 1000;
|
|
59
|
+
const REQUEST_FILES = new Set(["request.json", "delivery.json", "answer.json", "hook.json", "sending.json", "emitted.json", "failed.json", "closed.json"]);
|
|
60
|
+
/**
|
|
61
|
+
* 届かないまま残った依頼の置き場を消し、消した数を返す。誤りで返った呼び出しや打ち切られた呼び出しはPostToolUseが走らず、
|
|
62
|
+
* 置き場が残る。Windowsの一時置き場は再起動でも消えない。
|
|
63
|
+
* 消すのは、最後に書かれてから1日たち、待っている配送が無い置き場だけ。依頼元のClaude processが居て、配送を結んであり、
|
|
64
|
+
* まだ出し終えていない置き場は、何日たっても残す(子が長く動いている配送)。1日は、届かなかった時に原因を調べる間。
|
|
65
|
+
*/
|
|
66
|
+
export function sweepClaudeHookRequests(root, rows, now = Date.now()) {
|
|
67
|
+
let entries;
|
|
68
|
+
try {
|
|
69
|
+
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
let removed = 0;
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
if (!entry.isDirectory())
|
|
77
|
+
continue;
|
|
78
|
+
const dir = path.join(root, entry.name);
|
|
79
|
+
try {
|
|
80
|
+
if (now - fs.statSync(dir).mtimeMs < STALE_REQUEST_MS)
|
|
81
|
+
continue;
|
|
82
|
+
const names = fs.readdirSync(dir);
|
|
83
|
+
// この製品の依頼の置き場に見えない物には触れない。
|
|
84
|
+
if (!names.every(name => REQUEST_FILES.has(name) || name.endsWith(".tmp")))
|
|
85
|
+
continue;
|
|
86
|
+
if (names.some(name => now - fs.statSync(path.join(dir, name)).mtimeMs < STALE_REQUEST_MS))
|
|
87
|
+
continue;
|
|
88
|
+
if (names.includes("delivery.json") && !names.includes("emitted.json")) {
|
|
89
|
+
// 依頼元を読めない置き場には、もう誰も届けられない。
|
|
90
|
+
let invocation;
|
|
91
|
+
try {
|
|
92
|
+
invocation = invocationSchema.parse(JSON.parse(fs.readFileSync(path.join(dir, "request.json"), "utf8")));
|
|
93
|
+
}
|
|
94
|
+
catch { /* 消す */ }
|
|
95
|
+
if (invocation && rows.find(row => row.pid === invocation.parent_pid)?.started_identity === invocation.parent_started_identity)
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
99
|
+
removed += 1;
|
|
100
|
+
}
|
|
101
|
+
catch { /* 読めない・消せない置き場は次の見回りへ回す */ }
|
|
102
|
+
}
|
|
103
|
+
return removed;
|
|
50
104
|
}
|
|
51
105
|
/** 起動時envのsession IDは/clearで古くなるため、実際のPreToolUseとの相関だけを使う。 */
|
|
52
106
|
export function claudeParentFromRequest(profile, clientName, metadata, root) {
|
|
@@ -81,16 +135,30 @@ export function closeClaudeParentSession(input, root) {
|
|
|
81
135
|
const { session_id } = z.object({ session_id: z.uuid() }).parse(input);
|
|
82
136
|
if (!fs.existsSync(root))
|
|
83
137
|
return;
|
|
138
|
+
let failure;
|
|
84
139
|
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
85
140
|
if (!entry.isDirectory())
|
|
86
141
|
continue;
|
|
87
|
-
|
|
88
|
-
|
|
142
|
+
// 読めない記録(形の違う古い残り、送り手や見回りが消している途中の置き場)は飛ばす。1つで止まると、この会話の残りの依頼を閉じられない。
|
|
143
|
+
let invocation;
|
|
144
|
+
try {
|
|
145
|
+
invocation = invocationSchema.parse(JSON.parse(fs.readFileSync(path.join(root, entry.name, "request.json"), "utf8")));
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (invocation.session_id !== session_id)
|
|
89
151
|
continue;
|
|
90
|
-
|
|
91
|
-
if (invocation.session_id === session_id)
|
|
152
|
+
try {
|
|
92
153
|
writeJson0600(path.join(root, entry.name, "closed.json"), { session_id });
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (error.code !== "ENOENT")
|
|
157
|
+
failure ??= error;
|
|
158
|
+
}
|
|
93
159
|
}
|
|
160
|
+
if (failure)
|
|
161
|
+
throw failure;
|
|
94
162
|
}
|
|
95
163
|
function assertParentAlive(invocation) {
|
|
96
164
|
if (processIdentity(invocation.parent_pid) !== invocation.parent_started_identity) {
|
|
@@ -140,6 +208,12 @@ export async function submitClaudeParentAnswer(profile, parent, deliveryId, text
|
|
|
140
208
|
return true;
|
|
141
209
|
throw gone;
|
|
142
210
|
});
|
|
211
|
+
// 届いた依頼の置き場を読む物は、もう無い。届かなかった時は消さず、原因を調べる材料として残す。
|
|
212
|
+
// 片付けの失敗を配送の失敗にしない(消せなかった置き場は、後の見回りが消す)。
|
|
213
|
+
try {
|
|
214
|
+
await fs.promises.rm(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
|
|
215
|
+
}
|
|
216
|
+
catch { /* 見回りへ回す */ }
|
|
143
217
|
return { queued_submission_id: null };
|
|
144
218
|
}
|
|
145
219
|
export async function runClaudeResultHook(profile, input, emit, root) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aiterm-steer-delivery",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.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",
|