aiterm-steer-delivery 0.3.0 → 0.3.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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1
4
+
5
+ Codexの設定と起動の、2つの弱さを直します。APIと記録の形は変わりません。
6
+
7
+ - **登録の間に終わったCodexを、再起動待ちの記録(`stale_processes`)へ残さない。** `configureCodexSteer(profile, "enable", …)`は、登録より前から動いているCodexを再起動待ちとして記録する。ほかの導入が設定のために一時的に起こしたCodex(公式App Serverを起こして止める形)が、止まり切る前のprocessの一覧に載り、記録へ残っていた。承認の確かめの後にもう一度一覧を取り、居なくなった物を外してから保存する。返す状態(`ready`/`restart_required`)も、その同じ一覧で決める。
8
+ - 居ないprocessは、今までも照合で当たらない(pidと開始時刻の組で見る)。記録に残るだけで、配送は止まっていなかった。
9
+ - **Codexを起こす時、PATHにこのprocessのnodeの場所が無ければ足す。** npm版のCodexは、nodeで動く起動役(1行目が`#!/usr/bin/env node`)で、起こす側のPATHにnodeの場所が無いと起きない(exit 127)。素のsshの環境、製品の常駐process、アプリ配下のprocessから`withCodexReceiver`を呼ぶと、`CODEX_RECEIVER_TRANSPORT_FAILED`で断っていた。本文は送られていなかった。
10
+ - `withCodexReceiver`と、Codexの版を確かめる所が、起こす環境のPATHの頭へnodeの場所を足す。既にあれば、並びも中身も変えない。渡した`env`そのものは書き換えない。
11
+ - 同じ形の環境を自分で作る製品のために、`codexSpawnEnv(env?)`を公開する。
12
+ - Desktop同梱のCodex(macOS・Windows)は、元から当たらない。
13
+
3
14
  ## 0.3.0
4
15
 
5
16
  Codexの親へ、製品のhookを登録せずに届ける入口を足します。今までのAPIと記録の形は変わりません。
@@ -3,6 +3,12 @@ import type { CodexHookConfig } from "./codex-hook-state.js";
3
3
  export declare function realCodexHome(): string;
4
4
  /** 通常のCodex CLI。CODEX_BIN、~/.local/bin/codex、PATHの順に探す。 */
5
5
  export declare function resolveCodexExecutable(): string | null;
6
+ /**
7
+ * Codexを起こす時の環境。npm版のCodexは、nodeで動く起動役(1行目が`#!/usr/bin/env node`)で、起こす側のPATHにnodeの場所が
8
+ * 無いと起きない(exit 127。素のsshの環境、製品の常駐process、アプリ配下のprocessはPATHが細い事がある)。
9
+ * このprocessを動かしているnodeの場所を、PATHに無い時だけ頭へ足した写しを返す。既にあれば、並びも中身も変えない。
10
+ */
11
+ export declare function codexSpawnEnv(env?: NodeJS.ProcessEnv, node?: string, platform?: NodeJS.Platform): NodeJS.ProcessEnv;
6
12
  /**
7
13
  * Desktopが同梱するCodex CLI。2026-09のChatGPT.appは`Contents/Resources/codex`から
8
14
  * `Contents/Resources/codex-cli/CodexCLI.app/Contents/MacOS/codex`へ移した(macbook実測)。
@@ -87,8 +87,23 @@ export function resolveCodexExecutable() {
87
87
  }
88
88
  return null;
89
89
  }
90
+ /**
91
+ * Codexを起こす時の環境。npm版のCodexは、nodeで動く起動役(1行目が`#!/usr/bin/env node`)で、起こす側のPATHにnodeの場所が
92
+ * 無いと起きない(exit 127。素のsshの環境、製品の常駐process、アプリ配下のprocessはPATHが細い事がある)。
93
+ * このprocessを動かしているnodeの場所を、PATHに無い時だけ頭へ足した写しを返す。既にあれば、並びも中身も変えない。
94
+ */
95
+ export function codexSpawnEnv(env = process.env, node = process.execPath, platform = process.platform) {
96
+ const windows = platform === "win32";
97
+ // Windowsの環境変数名は大文字小文字を区別しない(`Path`で入っている事が多い)。
98
+ const key = Object.keys(env).find(name => windows ? name.toLowerCase() === "path" : name === "PATH") ?? "PATH";
99
+ const delimiter = windows ? ";" : ":";
100
+ const directory = (windows ? path.win32 : path.posix).dirname(node);
101
+ const current = env[key] ?? "";
102
+ const listed = current.split(delimiter).some(part => windows ? part.toLowerCase() === directory.toLowerCase() : part === directory);
103
+ return listed ? env : { ...env, [key]: current ? `${directory}${delimiter}${current}` : directory };
104
+ }
90
105
  function command(executable, args) {
91
- const result = spawnSync(executable, args, { encoding: "utf8", timeout: 15_000 });
106
+ const result = spawnSync(executable, args, { encoding: "utf8", timeout: 15_000, env: codexSpawnEnv() });
92
107
  if (result.error || result.status !== 0)
93
108
  throw new SetupError("codex_steer_setup_failed", `${path.basename(executable)}を実行できません`);
94
109
  return result.stdout.trim();
@@ -158,7 +173,7 @@ Join-Path $packages[0].InstallLocation 'app/resources'`, () => new CodexDelivery
158
173
  }
159
174
  /** CLIが対応する最低版(公式キューとhookの取込み)。 */
160
175
  function assertSteerVersion(executable, subject) {
161
- const result = spawnSync(executable, ["--version"], { encoding: "utf8", windowsHide: true, timeout: 15_000 });
176
+ const result = spawnSync(executable, ["--version"], { encoding: "utf8", windowsHide: true, timeout: 15_000, env: codexSpawnEnv() });
162
177
  if (result.error || result.status !== 0)
163
178
  throw new SetupError("codex_binary_unavailable", `${subject}を起動できません`);
164
179
  const match = /codex-cli (\d+)\.(\d+)\.(\d+)/.exec(result.stdout ?? "");
@@ -10,7 +10,10 @@ export interface CodexReceiverRuntime {
10
10
  args?: string[];
11
11
  timeout_ms?: number;
12
12
  hook_directory?: string;
13
- /** App Serverを起こす環境。省略すると、このprocessの環境。`CODEX_HOME`は常に`parent.codex_home`で上書きする。 */
13
+ /**
14
+ * App Serverを起こす環境。省略すると、このprocessの環境。`CODEX_HOME`は常に`parent.codex_home`で上書きする。
15
+ * PATHにこのprocessのnodeの場所が無い時は、頭へ足す(nodeで動くCodexの起動役を起こすため)。
16
+ */
14
17
  env?: NodeJS.ProcessEnv;
15
18
  }
16
19
  export declare function withCodexReceiver<T>(profile: ProductProfile, parent: CodexParent, action: (request: (method: string, params: unknown) => Promise<any>) => Promise<T>, runtime?: CodexReceiverRuntime): Promise<T>;
@@ -5,7 +5,7 @@ import { createInterface } from "node:readline";
5
5
  import * as path from "node:path";
6
6
  import { CodexDeliveryError } from "./errors.js";
7
7
  import { codexHookDirectory } from "./profile.js";
8
- import { currentCodexDesktopBinary, realCodexHome, resolveCodexExecutable } from "./codex-binary.js";
8
+ import { codexSpawnEnv, currentCodexDesktopBinary, realCodexHome, resolveCodexExecutable } from "./codex-binary.js";
9
9
  import { finishCodexHookSubmission, assertCodexHookParentCurrent, assertCodexHooksReady, readCodexHookConfig, registerCodexHookInput } from "./codex-hook-state.js";
10
10
  /** modelの引数ではなく、CodexがMCP要求へ付けるmetadataだけを宛先にする。 */
11
11
  export function codexParentFromRequest(clientName, metadata) {
@@ -24,7 +24,7 @@ export async function withCodexReceiver(profile, parent, action, runtime = {}) {
24
24
  throw new CodexDeliveryError("CODEX_RECEIVER_UNAVAILABLE", "Codexの実行ファイルを確認できません");
25
25
  const child = spawn(executable, runtime.args ?? ["app-server", "--listen", "stdio://"], {
26
26
  stdio: ["pipe", "pipe", "ignore"],
27
- env: { ...(runtime.env ?? process.env), CODEX_HOME: parent.codex_home },
27
+ env: { ...codexSpawnEnv(runtime.env ?? process.env), CODEX_HOME: parent.codex_home },
28
28
  windowsHide: true,
29
29
  });
30
30
  const pending = new Map();
@@ -296,9 +296,13 @@ export async function configureCodexSteer(profile, action = "status", overrides)
296
296
  const config = { schema: profile.codex_hook_schema, enabled: true,
297
297
  codex_home: runtime.codex_home, binary, command, node, hook: runtime.hook, stale_processes: stale };
298
298
  await runtime.verify(config, true);
299
+ // 登録の間に終わったprocessは、起き直しを待つ相手から外す。ほかの導入が設定のために一時的に起こしたCodex
300
+ // (公式App Serverを起こして止める形)が、止まり切る前の一覧に載る事がある(BellTeamの起動、2026-10-07)。
301
+ const alive = runtime.processes();
302
+ const saved = { ...config, stale_processes: config.stale_processes.filter(stale => alive.some(row => row.pid === stale.pid && row.started_identity === stale.started_identity)) };
299
303
  // 解除が中断しても、次のenableで移行を続行できるよう所有情報を先に保存する。
300
- save(config);
304
+ save(saved);
301
305
  if (legacy?.enabled)
302
306
  await runtime.disableLegacy();
303
- return needsRestart(config) ? { status: "restart_required", reason_code: "codex_restart_required" } : { status: "ready" };
307
+ return saved.stale_processes.length ? { status: "restart_required", reason_code: "codex_restart_required" } : { status: "ready" };
304
308
  }
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export { writeJson0600, writeHookJson, waitForFileState, withoutBom } from "./fi
4
4
  export { readRuntimeProcesses, parsePosixProcessTable, hookOwnerProcess, type RuntimeProcess, type NativeProcessIdentity } from "./process.js";
5
5
  export { resolveWindowsPowerShell7, quotePowerShell, windowsPowerShellSync, windowsStartProcessArgumentList } from "./windows.js";
6
6
  export { setupNodeExecutable } from "./setup-node.js";
7
- export { realCodexHome, resolveCodexExecutable, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, findLinuxDesktopBinary, findCliBinary, findSteerBinary, platformDesktopFinder, currentCodexDesktopBinary, type DesktopBinaryFinder, } from "./codex-binary.js";
7
+ export { realCodexHome, resolveCodexExecutable, codexSpawnEnv, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, findLinuxDesktopBinary, findCliBinary, findSteerBinary, platformDesktopFinder, currentCodexDesktopBinary, type DesktopBinaryFinder, } from "./codex-binary.js";
8
8
  export { codexHookConfigSchema, readCodexHookConfig, codexInputDirectory, hookInputSchema, answerDigest, registerCodexHookInput, finishCodexHookSubmission, codexHookDeliveryState, ownedCodexHooks, assertCodexHooksReady, assertCodexHookParentCurrent, type CodexHookConfig, } from "./codex-hook-state.js";
9
9
  export { codexParentFromRequest, withCodexReceiver, verifyCodexParent, submitCodexParentAnswer, type CodexParent, type CodexParentThread, type CodexReceiverRuntime, } from "./codex-receiver.js";
10
10
  export { runCodexResultHook } from "./codex-hooks.js";
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ export { writeJson0600, writeHookJson, waitForFileState, withoutBom } from "./fi
6
6
  export { readRuntimeProcesses, parsePosixProcessTable, hookOwnerProcess } from "./process.js";
7
7
  export { resolveWindowsPowerShell7, quotePowerShell, windowsPowerShellSync, windowsStartProcessArgumentList } from "./windows.js";
8
8
  export { setupNodeExecutable } from "./setup-node.js";
9
- export { realCodexHome, resolveCodexExecutable, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, findLinuxDesktopBinary, findCliBinary, findSteerBinary, platformDesktopFinder, currentCodexDesktopBinary, } from "./codex-binary.js";
9
+ export { realCodexHome, resolveCodexExecutable, codexSpawnEnv, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, findLinuxDesktopBinary, findCliBinary, findSteerBinary, platformDesktopFinder, currentCodexDesktopBinary, } from "./codex-binary.js";
10
10
  export { codexHookConfigSchema, readCodexHookConfig, codexInputDirectory, hookInputSchema, answerDigest, registerCodexHookInput, finishCodexHookSubmission, codexHookDeliveryState, ownedCodexHooks, assertCodexHooksReady, assertCodexHookParentCurrent, } from "./codex-hook-state.js";
11
11
  export { codexParentFromRequest, withCodexReceiver, verifyCodexParent, submitCodexParentAnswer, } from "./codex-receiver.js";
12
12
  export { runCodexResultHook } from "./codex-hooks.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiterm-steer-delivery",
3
- "version": "0.3.0",
3
+ "version": "0.3.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",