aiterm-steer-delivery 0.4.1 → 0.4.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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.2
4
+
5
+ Claude Codeが起動し直して同じ会話を再開した後、channelの本文が届かなかったのを直します。今までのAPIの返りは変わりません。
6
+
7
+ - **起きていた事。** channelは、開いた時のClaude Codeのprocess(pidと開始時刻)に結んである。待機(`runClaudeChannelWaiter`)は、その会話の開いているchannelのうち、結んだprocessが生きている物が1つも無いと、すぐ0で終わっていた。アプリが配下のClaude Codeを起こし直すと、同じ会話(同じ`session_id`)が新しいprocessで再開される。会話のchannelは全部、居ないprocessに結ばれたままなので、PostToolUseとStopの待機が毎回すぐ終わり、本文は受信箱に残った(製品が新しいchannelを開くまで届かない)。Windowsの実物(Claude Code 2.1.295)で起き、Linuxの実物(2.1.293)で再現した。
8
+ - **待機は、そのhookを起こしたClaude Codeのprocessが生きている間、続ける。** 本文を出す先はそのprocessなので、生き死にもそのprocessで見る。同じ会話(`session_id`)のhookを今走らせているprocessへ、前のprocessが開いたchannelの本文も出す。別の会話のchannelからは取らない(今までどおり、会話ごとの索引で分ける)。hookを起こしたprocessが居なくなったら、本文を取らずに終わる。hookを起こしたprocessを確かめられない時だけ、今までどおりchannelを開いた時のprocessの生き死にで決める。
9
+ - **会話の始まり(`SessionStart`)でも待機を張る。** channelを使う製品(`profile.channels`がある)の登録に、`SessionStart`のhook(`asyncRewake`)を足す。再開した会話が番を1つも回さないうちから、止まっていた間に届いた本文で起きる(実物で、再開した席が入力待ちになって3〜5秒後)。開いているchannelの無い会話(新しい会話、`/clear`の後)では、何もせず0で終わる。
10
+ - 0.4.1までの登録には`SessionStart`が無い。そのままでも配送は成り立ち(`claudeParentHooksRegistered`は`SessionStart`を求めない)、再開した会話は最初の番の終わりから受け取る。製品のsetup(`mergeClaudeParentHooks`)をかけ直すと`SessionStart`だけが足される。`removeClaudeParentHooks`は一緒に外す。
11
+ - channelを使わない製品(Aitermなど)の登録と動きは変わらない。
12
+ - **同じ会話に開いているchannelが幾つかある時、届いた順に出す。** 今まではchannelごと(IDの並び順)にまとめて出していた。起動し直す前のchannelと後のchannelの両方に本文がある時も、届いた順に並ぶ。
13
+ - `runClaudeChannelWaiter`の`options`に`owner`(試験が、hookを起こしたprocessを差し替える口)を足す。
14
+ - 0.4.1の記述の言い足し: Linuxのアプリで寝ている会話が起きなかった端末は、アプリのログインの期限が切れていた(アプリの記録に`token_expired`)。リンクを開く口が効かないのか、ログインのせいかは、まだ分けられていない。Linuxで見張りを起こさない事は変わらない。
15
+
3
16
  ## 0.4.1
4
17
 
5
18
  0.4.0の「寝ている会話を起こす見張り」の、3つの直しです。
package/README.md CHANGED
@@ -185,7 +185,7 @@ await steer.sendToChannel(PROFILE, channel.channel_id, randomUUID(), text);
185
185
  steer.channelDeliveryState(PROFILE, channel.channel_id, id); // queued | sending | emitted | unknown | withdrawn
186
186
  ```
187
187
 
188
- Claude Code re-arms its waiter at every `Stop`. Cursor binds the channel with `steer.channelMarker(channel)` placed in your tool result, and idle Cursor/Grok parents run `steer.channelReceiveProcess(...)` in the background; each result carries `next_wait_process` to re-arm. `withdrawFromChannel` takes back text no receiver has claimed yet, so you can resend it on a new channel; `closeChannel` ends one.
188
+ Claude Code re-arms its waiter at every `Stop`, and arms it at `SessionStart` as well. The waiter belongs to the Claude Code process that ran the hook: it keeps waiting while that process is alive, and it delivers every open channel of that conversation (`session_id`), including channels opened by an earlier process. So when Claude Code restarts and resumes the same conversation in a new process, messages that arrived while it was down wake the resumed conversation, even before it runs a turn. (A registration written by 0.4.1 or earlier has no `SessionStart` hook; it still works, and the resumed conversation starts receiving at the end of its first turn. Run your setup again to add the hook.) Cursor binds the channel with `steer.channelMarker(channel)` placed in your tool result, and idle Cursor/Grok parents run `steer.channelReceiveProcess(...)` in the background; each result carries `next_wait_process` to re-arm. `withdrawFromChannel` takes back text no receiver has claimed yet, so you can resend it on a new channel; `closeChannel` ends one.
189
189
 
190
190
  Put both the marker and the receiver command in the **text** of your tool result, not only in `structuredContent`: the Cursor CLI model does not read structured content. `steer.waitProcessCommandLine(steer.channelReceiveProcess(script, channel.channel_id))` gives the command as one shell line (POSIX sh, or PowerShell on Windows).
191
191
 
package/dist/channel.d.ts CHANGED
@@ -58,10 +58,19 @@ export declare function withdrawFromChannel(profile: ProductProfile, channelId:
58
58
  /**
59
59
  * Claude Codeのhook(PostToolUse・Stop、asyncRewake)から呼ぶ。その会話のchannelに届いた本文を待ち、
60
60
  * 出力したら2(親を起こす)、待つものが無い・他の待機が生きている・会話が終わったなら0を返す。
61
+ *
62
+ * 待機は、このhookを起こしたClaude Codeのprocessが生きている間だけ続ける。本文はそのprocessへ出すので、居なくなった後に取ると失う。
63
+ * channelを開いた時のprocess(`channel.claude.parent_pid`)が居なくなっていても、同じ会話(session_id)のhookを走らせている今のprocessへ出す。
64
+ * Claude Codeが起動し直して同じ会話を再開した後も、前のprocessが開いたchannelの本文が届く。
65
+ * hookを起こしたprocessを確かめられない時だけ、channelを開いた時のprocessの生き死にで決める。
61
66
  */
62
67
  export declare function runClaudeChannelWaiter(profile: ProductProfile, input: unknown, emit: (text: string) => Promise<void> | void, options?: {
63
68
  wait_ms?: number;
64
69
  poll_ms?: number;
70
+ owner?: {
71
+ pid: number;
72
+ started_identity: string;
73
+ } | null;
65
74
  }): Promise<0 | 2>;
66
75
  /** Claude CodeのSessionEndで、その会話のchannelを閉じる。未取得の本文は残る。 */
67
76
  export declare function closeClaudeSessionChannels(profile: ProductProfile, input: unknown): void;
package/dist/channel.js CHANGED
@@ -12,7 +12,7 @@ import { randomUUID } from "node:crypto";
12
12
  import { setTimeout as delay } from "node:timers/promises";
13
13
  import { z } from "zod";
14
14
  import { writeJson0600 } from "./files.js";
15
- import { readRuntimeProcesses } from "./process.js";
15
+ import { hookOwnerProcess, readRuntimeProcesses } from "./process.js";
16
16
  import { channelRoot } from "./profile.js";
17
17
  import { SteerDeliveryError } from "./errors.js";
18
18
  import { submitCodexParentAnswer } from "./codex-receiver.js";
@@ -302,14 +302,25 @@ function releaseWaiter(index, identity) {
302
302
  }
303
303
  catch { /* 既に無い */ }
304
304
  }
305
+ /** このhookを起こしたClaude Codeのprocess。shellを通す形のhookでは、直接の親のshellの先をたどる。分からなければnull。 */
306
+ function currentHookOwner() {
307
+ const owner = hookOwnerProcess(readRuntimeProcesses(), process.ppid);
308
+ return owner ? { pid: owner.pid, started_identity: owner.started_identity } : null;
309
+ }
305
310
  /**
306
311
  * Claude Codeのhook(PostToolUse・Stop、asyncRewake)から呼ぶ。その会話のchannelに届いた本文を待ち、
307
312
  * 出力したら2(親を起こす)、待つものが無い・他の待機が生きている・会話が終わったなら0を返す。
313
+ *
314
+ * 待機は、このhookを起こしたClaude Codeのprocessが生きている間だけ続ける。本文はそのprocessへ出すので、居なくなった後に取ると失う。
315
+ * channelを開いた時のprocess(`channel.claude.parent_pid`)が居なくなっていても、同じ会話(session_id)のhookを走らせている今のprocessへ出す。
316
+ * Claude Codeが起動し直して同じ会話を再開した後も、前のprocessが開いたchannelの本文が届く。
317
+ * hookを起こしたprocessを確かめられない時だけ、channelを開いた時のprocessの生き死にで決める。
308
318
  */
309
319
  export async function runClaudeChannelWaiter(profile, input, emit, options = {}) {
310
320
  const event = z.object({ session_id: z.uuid() }).loose().parse(input);
311
321
  if (!openClaudeChannels(profile, event.session_id).length)
312
322
  return 0;
323
+ const owner = options.owner === undefined ? currentHookOwner() : options.owner;
313
324
  const index = sessionIndex(profile, event.session_id);
314
325
  const identity = acquireWaiter(index);
315
326
  if (!identity)
@@ -323,11 +334,15 @@ export async function runClaudeChannelWaiter(profile, input, emit, options = {})
323
334
  return 0;
324
335
  if (Date.now() - lastParentCheck >= 5_000) {
325
336
  lastParentCheck = Date.now();
326
- // 依頼元のClaude processが終わっていたら、本文を別の会話へ流さずに待機を終える。
327
- if (!channels.some(channel => processIdentity(channel.claude.parent_pid) === channel.claude.parent_started_identity))
337
+ // 本文を出す先(このhookを起こしたClaude process)が終わっていたら、本文を取らずに待機を終える。
338
+ const alive = owner ? processIdentity(owner.pid) === owner.started_identity
339
+ : channels.some(channel => processIdentity(channel.claude.parent_pid) === channel.claude.parent_started_identity);
340
+ if (!alive)
328
341
  return 0;
329
342
  }
330
- const claimed = channels.flatMap(channel => claimPending(profile, channel.channel_id, "claude_hook"));
343
+ // 同じ会話に開いているchannelが幾つかある時(起動し直す前のchannelと後のchannel)も、届いた順に出す。
344
+ const claimed = channels.flatMap(channel => claimPending(profile, channel.channel_id, "claude_hook"))
345
+ .sort((left, right) => left.item.at < right.item.at ? -1 : left.item.at > right.item.at ? 1 : 0);
331
346
  if (claimed.length) {
332
347
  await emitClaimed(claimed, "claude_hook", texts => emit(texts.join("\n\n")));
333
348
  return 2;
package/dist/hook-main.js CHANGED
@@ -56,6 +56,13 @@ export async function runClaudeHookMain(profile, root = claudeHookRoot(profile))
56
56
  throw new Error("CLAUDE_PARENT_HOOK_EVENT_INVALID: 未対応のhookです");
57
57
  process.exitCode = await runClaudeChannelWaiter(profile, event, write);
58
58
  break;
59
+ case "SessionStart":
60
+ // 会話が起動し直して再開された時は、番が1つも回らないうちから、前のprocessが開いたchannelの待機を張る。
61
+ // 開いているchannelが無い会話(新しい会話、/clearの後)では、何もせず0で終わる。
62
+ if (!profile.channels)
63
+ throw new Error("CLAUDE_PARENT_HOOK_EVENT_INVALID: 未対応のhookです");
64
+ process.exitCode = await runClaudeChannelWaiter(profile, event, write);
65
+ break;
59
66
  case "SessionEnd":
60
67
  closeClaudeParentSession(event, root);
61
68
  if (profile.channels)
@@ -20,7 +20,10 @@ export declare function claudeParentHookEntries(profile: ProductProfile, hook: H
20
20
  hooks: Record<string, unknown>[];
21
21
  }[]>;
22
22
  export declare function mergeClaudeParentHooks(profile: ProductProfile, file: string, hook: HookRuntime): "configured" | "unchanged";
23
- /** 製品が登録する全てのeventに、当製品のhookがあるか(設定を読むだけ。形は旧・新のどちらでもよい)。 */
23
+ /**
24
+ * 配送に要る全てのeventに、当製品のhookがあるか(設定を読むだけ。形は旧・新のどちらでもよい)。
25
+ * SessionStartは求めない。無い登録(0.4.1まで)でも配送は成り立ち、起動し直した会話は最初の番の終わりから受け取る。
26
+ */
24
27
  export declare function claudeParentHooksRegistered(profile: ProductProfile, document: unknown): boolean;
25
28
  /** 登録済みの当製品のhookが指す入口のpath。入口のfileが残っているかを製品が確かめるために使う。 */
26
29
  export declare function claudeParentHookScripts(profile: ProductProfile, document: unknown): string[];
@@ -34,10 +34,17 @@ export function claudeParentHookEntries(profile, hook, platform = process.platfo
34
34
  PreToolUse: [{ matcher, hooks: [{ ...command, timeout: 15 }] }],
35
35
  PostToolUse: [{ matcher, hooks: [{ ...command, asyncRewake: true, timeout: 86400 }] }],
36
36
  // 長く続く受信(channel)では、turnが終わるたびに待機を張り直す。
37
- ...(profile.channels ? { Stop: [{ hooks: [{ ...command, asyncRewake: true, timeout: 86400 }] }] } : {}),
37
+ // 会話の始まり(SessionStart)でも張る。Claude Codeが起動し直して同じ会話を再開した時、番が1つも回らないうちから、
38
+ // 前のprocessが開いたchannelの本文を受け取れる。開いているchannelの無い会話では、何もせず終わる。
39
+ ...(profile.channels ? {
40
+ Stop: [{ hooks: [{ ...command, asyncRewake: true, timeout: 86400 }] }],
41
+ SessionStart: [{ hooks: [{ ...command, asyncRewake: true, timeout: 86400 }] }],
42
+ } : {}),
38
43
  SessionEnd: [{ hooks: [{ ...command, timeout: 15 }] }],
39
44
  };
40
45
  }
46
+ /** 無くても配送が成り立つevent。0.4.1までの登録には無いので、登録の確認では求めない(次のsetupで足される)。 */
47
+ const OPTIONAL_CLAUDE_EVENTS = new Set(["SessionStart"]);
41
48
  export function mergeClaudeParentHooks(profile, file, hook) {
42
49
  const target = existsSync(file) ? realpathSync(file) : file;
43
50
  let current = {};
@@ -110,9 +117,13 @@ function ownedClaudeParentHooks(profile, document, event) {
110
117
  return [];
111
118
  return hooks.flatMap(group => record(group) && Array.isArray(group.hooks) ? group.hooks : []).filter(entry => isClaudeParentHook(profile, entry));
112
119
  }
113
- /** 製品が登録する全てのeventに、当製品のhookがあるか(設定を読むだけ。形は旧・新のどちらでもよい)。 */
120
+ /**
121
+ * 配送に要る全てのeventに、当製品のhookがあるか(設定を読むだけ。形は旧・新のどちらでもよい)。
122
+ * SessionStartは求めない。無い登録(0.4.1まで)でも配送は成り立ち、起動し直した会話は最初の番の終わりから受け取る。
123
+ */
114
124
  export function claudeParentHooksRegistered(profile, document) {
115
125
  return Object.keys(claudeParentHookEntries(profile, { command: "", script: profile.hooks.claude }))
126
+ .filter(event => !OPTIONAL_CLAUDE_EVENTS.has(event))
116
127
  .every(event => ownedClaudeParentHooks(profile, document, event).length > 0);
117
128
  }
118
129
  /** 登録済みの当製品のhookが指す入口のpath。入口のfileが残っているかを製品が確かめるために使う。 */
@@ -134,7 +145,7 @@ export function removeClaudeParentHooks(profile, file) {
134
145
  if (current.hooks === undefined)
135
146
  return "unchanged";
136
147
  const hooks = { ...current.hooks };
137
- for (const event of ["PreToolUse", "PostToolUse", "Stop", "SessionEnd"]) {
148
+ for (const event of ["PreToolUse", "PostToolUse", "Stop", "SessionStart", "SessionEnd"]) {
138
149
  if (hooks[event] === undefined)
139
150
  continue;
140
151
  if (!Array.isArray(hooks[event]))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiterm-steer-delivery",
3
- "version": "0.4.1",
3
+ "version": "0.4.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",