aiterm-steer-delivery 0.1.2 → 0.1.4

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,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.4
4
+
5
+ - CodexのSteerのhookを外した後、自製品のhookが居た位置の承認記録(`hooks.state`の`trusted_hash`・`enabled`)が`config.toml`に残っていた(ラプンツェル氏の報告)。他のhookが入らずに空いた位置の記録を消す。別のfileや他の位置の記録は触らない。記録の後片付けが公式APIの都合でできなくても、hookの解除そのものは止めない。
6
+
7
+ ## 0.1.3
8
+
9
+ - Codex CLIをsymlink経由で起動した形(`node <symlinkのpath>`)も、再起動待ちとして照合する。
10
+ - macOS・Windowsで失敗していた試験を直した(試験のコマンド行の書き方と、macOSの一時ディレクトリの実体path)。0.1.1・0.1.2のmainのCIはこの試験でmacOS・Windowsが失敗していた。
11
+
3
12
  ## 0.1.2
4
13
 
5
14
  - CodexのSteer(作業中のturnへの差し込み)を、Codex Desktopの無い端末とLinuxにも広げた(指揮官氏の判断 2026-09-29)。公式Desktopの同梱CLI(macOS・Windows・Linux)を先に探し、無ければ通常のCodex CLI(0.154以上)を使う。以前はmacOS・WindowsのDesktopだけで、Linuxは`unsupported`だった。
@@ -19,6 +19,10 @@ export type CodexHookPlan = {
19
19
  next: any;
20
20
  changed: boolean;
21
21
  moves: CodexHookMove[];
22
+ owned: {
23
+ event: string;
24
+ at: [number, number];
25
+ }[];
22
26
  };
23
27
  /** 自製品のhookを置換・解除した後のhooks.jsonと、他のhookの位置の動きを計算する(書き込まない)。 */
24
28
  export declare function planCodexParentHooks(file: string, command: string | null, previousCommand?: string): CodexHookPlan;
@@ -45,6 +45,7 @@ export function planCodexParentHooks(file, command, previousCommand) {
45
45
  throw new SetupError("codex_hook_config_invalid", "Codexのhooks設定を読めません");
46
46
  const hooks = { ...current.hooks };
47
47
  const moves = [];
48
+ const ownedAt = [];
48
49
  for (const event of ["PostToolUse", "Stop"]) {
49
50
  const groups = hooks[event] ?? [];
50
51
  if (!Array.isArray(groups) || groups.some(group => !object(group) || !Array.isArray(group.hooks)))
@@ -52,7 +53,9 @@ export function planCodexParentHooks(file, command, previousCommand) {
52
53
  const owned = (hook) => hook?.type === "command" && ((command !== null && hook.command === command) || (!!previousCommand && hook.command === previousCommand));
53
54
  const before = new Map();
54
55
  groups.forEach((group, g) => group.hooks.forEach((hook, h) => { if (!owned(hook))
55
- before.set(hook, [g, h]); }));
56
+ before.set(hook, [g, h]);
57
+ else
58
+ ownedAt.push({ event, at: [g, h] }); }));
56
59
  // 既存の位置で置換する。末尾へ移すと同じ設定の再導入でも変更扱いになり、
57
60
  // 稼働中のCodexへ不要な再起動を要求してしまう。
58
61
  hooks[event] = [];
@@ -79,7 +82,7 @@ export function planCodexParentHooks(file, command, previousCommand) {
79
82
  delete hooks[event];
80
83
  }
81
84
  const next = { ...current, hooks };
82
- return { target, current, next, changed: !isDeepStrictEqual(current, next), moves };
85
+ return { target, current, next, changed: !isDeepStrictEqual(current, next), moves, owned: ownedAt };
83
86
  }
84
87
  export function writeCodexHookPlan(profile, plan) {
85
88
  if (!plan.changed)
@@ -102,8 +105,17 @@ const snake = (event) => event.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCas
102
105
  export async function applyCodexHookPlan(profile, plan, codexHome, executable) {
103
106
  if (!plan.changed)
104
107
  return false;
105
- if (!plan.moves.length)
106
- return writeCodexHookPlan(profile, plan);
108
+ // 他のhookが動かず、自製品のhookを外すだけの時は、書き換えてから空いた位置の承認を消す。
109
+ // 承認の後片付けが公式APIの都合でできなくても、hookの解除そのものは止めない。
110
+ if (!plan.moves.length) {
111
+ writeCodexHookPlan(profile, plan);
112
+ if (plan.owned.length) {
113
+ await cleanVacatedTrust(profile, plan, codexHome, executable).catch(error => {
114
+ process.stderr.write(`${profile.id}: 外したhookの承認記録を消せませんでした(${error instanceof Error ? error.message : String(error)})\n`);
115
+ });
116
+ }
117
+ return true;
118
+ }
107
119
  return withCodexReceiver(profile, { thread_id: "00000000-0000-4000-8000-000000000000", codex_home: codexHome }, async (request) => {
108
120
  const listed = await request("hooks/list", { cwds: [codexHome] });
109
121
  const source = fs.realpathSync(plan.target);
@@ -129,12 +141,45 @@ export async function applyCodexHookPlan(profile, plan, codexHome, executable) {
129
141
  const used = new Set();
130
142
  for (const event of ["PostToolUse", "Stop"])
131
143
  (plan.next.hooks?.[event] ?? []).forEach((group, g) => group.hooks.forEach((_, h) => used.add(key(event, [g, h]))));
132
- const vacated = plan.moves.map(move => key(move.event, move.from)).filter(old => !used.has(old) && state[old] !== undefined);
144
+ const vacated = [...plan.moves.map(move => key(move.event, move.from)), ...plan.owned.map(item => key(item.event, item.at))]
145
+ .filter((old, index, all) => all.indexOf(old) === index && !used.has(old) && state[old] !== undefined);
133
146
  if (vacated.length)
134
147
  await request("config/batchWrite", { edits: vacated.map(old => ({ keyPath: `hooks.state.${JSON.stringify(old)}`, value: null, mergeStrategy: "replace" })), filePath: configFile });
135
148
  return true;
136
149
  }, executable ? { executable } : {});
137
150
  }
151
+ /** hooks.jsonを書き換えた後、自製品のhookが居た位置で空いたものの承認記録を消す。 */
152
+ async function cleanVacatedTrust(profile, plan, codexHome, executable) {
153
+ await withCodexReceiver(profile, { thread_id: "00000000-0000-4000-8000-000000000000", codex_home: codexHome }, async (request) => {
154
+ const source = fs.realpathSync(plan.target);
155
+ const prefix = `${source}`;
156
+ const key = (event, [g, h]) => `${prefix}:${snake(event)}:${g}:${h}`;
157
+ const state = (await request("config/read", { includeLayers: false }))?.config?.hooks?.state ?? {};
158
+ const used = new Set();
159
+ for (const event of ["PostToolUse", "Stop"])
160
+ (plan.next.hooks?.[event] ?? []).forEach((group, g) => group.hooks.forEach((_, h) => used.add(key(event, [g, h]))));
161
+ // 鍵のfile部分はCodexの表記(hooks/listのkey)に合わせる。記録にある鍵のうち、同じfileを指すものだけを対象にする。
162
+ const vacated = Object.keys(state).filter(stored => plan.owned.some(item => stored.endsWith(`:${snake(item.event)}:${item.at[0]}:${item.at[1]}`)
163
+ && samePath(stored.split(":").slice(0, -3).join(":"), source)) && !used.has(key(...parseSuffix(stored))));
164
+ if (vacated.length)
165
+ await request("config/batchWrite", { edits: vacated.map(old => ({ keyPath: `hooks.state.${JSON.stringify(old)}`, value: null, mergeStrategy: "replace" })), filePath: path.join(codexHome, "config.toml") });
166
+ }, executable ? { executable } : {});
167
+ }
168
+ function parseSuffix(stored) {
169
+ const parts = stored.split(":");
170
+ const h = Number(parts.pop()), g = Number(parts.pop()), event = parts.pop();
171
+ return [event === "post_tool_use" ? "PostToolUse" : event === "stop" ? "Stop" : event, [g, h]];
172
+ }
173
+ function samePath(a, b) {
174
+ if (a === b)
175
+ return true;
176
+ try {
177
+ return fs.realpathSync(a) === fs.realpathSync(b);
178
+ }
179
+ catch {
180
+ return process.platform === "win32" && a.toLowerCase() === b.toLowerCase();
181
+ }
182
+ }
138
183
  export async function verifyCodexHookRegistration(profile, config, approve, directory = codexHookDirectory(profile)) {
139
184
  fs.accessSync(config.node, fs.constants.X_OK);
140
185
  fs.accessSync(config.hook, fs.constants.R_OK);
@@ -214,8 +259,10 @@ export async function configureCodexSteer(profile, action = "status", overrides)
214
259
  const stale = !previous?.enabled || changed || legacy?.enabled
215
260
  ? runtime.processes().filter(row => binaries.some(candidate => {
216
261
  const command = normalize(row.command);
262
+ // 実行ファイルそのもの、またはnode等に渡された引数として(`node <path>`)現れるもの。
217
263
  return (row.executable !== undefined && normalize(row.executable) === candidate) || command === candidate || command === `"${candidate}"`
218
- || command.startsWith(candidate + " ") || command.startsWith(`"${candidate}" `);
264
+ || command.startsWith(candidate + " ") || command.startsWith(`"${candidate}" `)
265
+ || command.endsWith(" " + candidate) || command.includes(` ${candidate} `) || command.includes(` "${candidate}"`);
219
266
  }) || roots.some(root => normalize(row.command).includes(root + (runtime.platform === "win32" ? "\\" : "/"))))
220
267
  .map(({ pid, started_identity }) => ({ pid, started_identity }))
221
268
  : previous.stale_processes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiterm-steer-delivery",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
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",