cdx-chores 0.1.8-canary.3 → 0.1.8

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/README.md CHANGED
@@ -82,8 +82,24 @@ Use `cdx-chores doctor` before relying on a command in a script, a CI job, or a
82
82
  | `data extract` reviewed suggestions, `data query codex` | Codex-assisted source shaping, semantic header review, and natural-language SQL drafting | Codex support must be configured and an auth/session signal must be available | Run `cdx-chores doctor` |
83
83
  | `md pdf-profile codex`, `md pdf-template codex`, `md pdf-project codex` | Codex-assisted Markdown PDF profile, template, and coordinated project drafting | Codex support must be configured for Codex-assisted decisions; deterministic fallback paths remain available where documented | Run `cdx-chores doctor` |
84
84
 
85
- Codex-assisted workflows in `v0.1.8-canary.3` are validated with
86
- `@openai/codex-sdk` `0.153.3`.
85
+ The Codex SDK baseline for `v0.1.8` is `@openai/codex-sdk` `0.153.4`.
86
+
87
+ Adopted helper commands and explicit `interactive` sessions accept
88
+ `--codex-model`, `--codex-provider`, and `--codex-reasoning-effort`. Model and
89
+ provider inherit Codex configuration when omitted; helper reasoning defaults
90
+ to `low`. See [Codex execution configuration](docs/guides/codex-execution-configuration.md)
91
+ for command scope and failure behavior.
92
+
93
+ Inspect the configuration and reported catalog without drafting a helper result:
94
+
95
+ ```bash
96
+ cdx-chores codex-info
97
+ cdx-chores codex-info models --details
98
+ cdx-chores codex-info providers --json
99
+ ```
100
+
101
+ Provider discovery lists configured definitions only; built-ins are not
102
+ enumerated. Model metadata does not verify support through the selected provider.
87
103
 
88
104
  Markdown PDF profile, template, and Codex-assisted profile/template/project
89
105
  helpers remain available as direct CLI flows. Interactive mode also provides
@@ -415,6 +431,8 @@ cdx-chores video resize -i ./clip.mp4 -o ./clip-720p.mp4 --width 1280 --height 7
415
431
 
416
432
  Cross-feature:
417
433
 
434
+ - [Codex execution configuration and discovery](docs/guides/codex-execution-configuration.md)
435
+ - [Environment variables](docs/guides/environment-variables.md)
418
436
  - `docs/guides/cli-output-and-color.md`
419
437
  - `docs/guides/codex-timeouts-retries-and-recovery.md`
420
438
  - `docs/guides/patterns-placeholders-and-templates.md`
@@ -459,6 +477,16 @@ Install dependencies:
459
477
  bun install
460
478
  ```
461
479
 
480
+ Run unit tests, or all suites when their prerequisites are available:
481
+
482
+ ```bash
483
+ bun run test:unit
484
+ bun run test:all
485
+ ```
486
+
487
+ See the [Contributor Testing Guide](docs/guides/testing.md) for suite selection,
488
+ platform requirements, prerequisites, focused testing, and retained results.
489
+
462
490
  Build the package:
463
491
 
464
492
  ```bash
@@ -0,0 +1,291 @@
1
+ const require_rolldown_runtime = require("./rolldown-runtime-emK7D4bc.cjs");
2
+ let node_fs = require("node:fs");
3
+ let node_path = require("node:path");
4
+ let node_child_process = require("node:child_process");
5
+ let node_module = require("node:module");
6
+ node_module = require_rolldown_runtime.__toESM(node_module, 1);
7
+ let node_string_decoder = require("node:string_decoder");
8
+ //#region src/adapters/codex/discovery/executable.ts
9
+ function hasType(path, type) {
10
+ try {
11
+ const stat = (0, node_fs.statSync)(path);
12
+ return type === "file" ? stat.isFile() : stat.isDirectory();
13
+ } catch {
14
+ return false;
15
+ }
16
+ }
17
+ function resolveSdkPackage() {
18
+ if (typeof node_module.findPackageJSON === "function") {
19
+ const path = node_module.findPackageJSON("@openai/codex-sdk", require("url").pathToFileURL(__filename).href);
20
+ if (path) return path;
21
+ }
22
+ const localRequire = node_module.createRequire(require("url").pathToFileURL(__filename).href);
23
+ for (const directory of localRequire.resolve.paths("@openai/codex-sdk") ?? []) {
24
+ const path = (0, node_path.join)(directory, "@openai/codex-sdk/package.json");
25
+ if (hasType(path, "file")) return (0, node_fs.realpathSync)(path);
26
+ }
27
+ throw new Error();
28
+ }
29
+ /** Resolve the SDK's installed optional binary without accessing SDK internals. */
30
+ function resolveDiscoveryExecutable(env) {
31
+ const override = env.CDX_CHORES_CODEX_PATH?.trim();
32
+ if (override) return override;
33
+ const platform = process.platform === "android" ? "linux" : process.platform;
34
+ const targetPlatform = {
35
+ linux: "unknown-linux-musl",
36
+ darwin: "apple-darwin",
37
+ win32: "pc-windows-msvc"
38
+ }[platform];
39
+ const targetArch = {
40
+ x64: "x86_64",
41
+ arm64: "aarch64"
42
+ }[process.arch];
43
+ if (!targetPlatform || !targetArch) throw new Error("Codex discovery is unsupported on this platform.");
44
+ try {
45
+ const sdkRequire = node_module.createRequire(resolveSdkPackage());
46
+ const root = (0, node_path.join)((0, node_path.dirname)(node_module.createRequire(sdkRequire.resolve("@openai/codex/package.json")).resolve(`@openai/codex-${platform}-${process.arch}/package.json`)), "vendor", `${targetArch}-${targetPlatform}`);
47
+ const name = platform === "win32" ? "codex.exe" : "codex";
48
+ const modern = (0, node_path.join)(root, "bin", name);
49
+ const isModern = hasType(modern, "file") && hasType((0, node_path.join)(root, "codex-package.json"), "file");
50
+ const binary = isModern ? modern : (0, node_path.join)(root, "codex", name);
51
+ if (!hasType(binary, "file")) throw new Error();
52
+ const pathDirectory = (0, node_path.join)(root, isModern ? "codex-path" : "path");
53
+ if (hasType(pathDirectory, "directory")) {
54
+ const pathKeys = Object.keys(env).filter((key) => key.toLowerCase() === "path");
55
+ const pathKey = platform === "win32" ? pathKeys.includes("Path") ? "Path" : pathKeys.at(-1) ?? "PATH" : "PATH";
56
+ const previous = env[pathKey] ?? "";
57
+ if (platform === "win32") {
58
+ for (const key of pathKeys) if (key !== pathKey) delete env[key];
59
+ }
60
+ env[pathKey] = [pathDirectory, ...previous.split(node_path.delimiter).filter((entry) => entry !== "" && entry !== pathDirectory)].join(node_path.delimiter);
61
+ }
62
+ return binary;
63
+ } catch {
64
+ throw new Error("Unable to locate the installed Codex binary; install its optional dependencies or set CDX_CHORES_CODEX_PATH.");
65
+ }
66
+ }
67
+ //#endregion
68
+ //#region src/adapters/codex/discovery/transport.ts
69
+ const MAX_LINE_BYTES = 1024 * 1024;
70
+ const MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
71
+ function isRecord(value) {
72
+ return value !== null && typeof value === "object" && !Array.isArray(value);
73
+ }
74
+ /** One owned app-server; requests are intentionally sequential. */
75
+ var DiscoveryTransport = class {
76
+ child;
77
+ exited;
78
+ deadline;
79
+ signal;
80
+ onAbort = () => this.fail(/* @__PURE__ */ new Error("Codex discovery was cancelled."));
81
+ pending;
82
+ failure;
83
+ nextId = 1;
84
+ stopping = false;
85
+ hasExited = false;
86
+ constructor(executable, cwd, env, timeoutMs, signal) {
87
+ try {
88
+ this.child = (0, node_child_process.spawn)(executable, ["app-server"], {
89
+ cwd,
90
+ env,
91
+ stdio: "pipe",
92
+ windowsHide: true
93
+ });
94
+ } catch {
95
+ throw new Error("Unable to start the Codex discovery process.");
96
+ }
97
+ this.signal = signal;
98
+ this.deadline = setTimeout(() => this.fail(/* @__PURE__ */ new Error("Codex discovery timed out.")), timeoutMs);
99
+ this.exited = new Promise((resolve) => {
100
+ const ended = () => {
101
+ this.hasExited = true;
102
+ if (!this.stopping) this.fail(/* @__PURE__ */ new Error("Codex discovery process ended unexpectedly."));
103
+ resolve();
104
+ };
105
+ this.child.once("exit", ended);
106
+ this.child.once("error", () => {
107
+ this.fail(/* @__PURE__ */ new Error("Unable to start the Codex discovery process."));
108
+ ended();
109
+ });
110
+ });
111
+ signal?.addEventListener("abort", this.onAbort, { once: true });
112
+ if (signal?.aborted) this.onAbort();
113
+ this.child.stdin.on("error", () => this.fail(/* @__PURE__ */ new Error("Codex discovery input failed.")));
114
+ this.child.stdout.on("error", () => this.fail(/* @__PURE__ */ new Error("Codex discovery output failed.")));
115
+ this.child.stderr.on("error", () => this.fail(/* @__PURE__ */ new Error("Codex discovery output failed.")));
116
+ let stderrBytes = 0;
117
+ this.child.stderr.on("data", (chunk) => {
118
+ stderrBytes += chunk.length;
119
+ if (stderrBytes > MAX_OUTPUT_BYTES) this.fail(/* @__PURE__ */ new Error("Codex discovery exceeded its output limit."));
120
+ });
121
+ const decoder = new node_string_decoder.StringDecoder("utf8");
122
+ let buffer = "";
123
+ let totalBytes = 0;
124
+ this.child.stdout.on("data", (chunk) => {
125
+ if (this.failure || this.stopping) return;
126
+ totalBytes += chunk.length;
127
+ if (totalBytes > MAX_OUTPUT_BYTES) {
128
+ this.fail(/* @__PURE__ */ new Error("Codex discovery exceeded its output limit."));
129
+ return;
130
+ }
131
+ buffer += decoder.write(chunk);
132
+ let newline;
133
+ while ((newline = buffer.indexOf("\n")) !== -1) {
134
+ const line = buffer.slice(0, newline);
135
+ buffer = buffer.slice(newline + 1);
136
+ if (Buffer.byteLength(line) > MAX_LINE_BYTES) {
137
+ this.fail(/* @__PURE__ */ new Error("Codex discovery exceeded its response limit."));
138
+ return;
139
+ }
140
+ this.receive(line);
141
+ if (this.failure) return;
142
+ }
143
+ if (Buffer.byteLength(buffer) > MAX_LINE_BYTES) this.fail(/* @__PURE__ */ new Error("Codex discovery exceeded its response limit."));
144
+ });
145
+ this.child.stdout.on("end", () => {
146
+ if (!this.stopping) this.fail(/* @__PURE__ */ new Error("Codex discovery output ended unexpectedly."));
147
+ });
148
+ }
149
+ fail(error) {
150
+ if (this.failure || this.stopping) return;
151
+ this.failure = error;
152
+ this.pending?.reject(error);
153
+ this.pending = void 0;
154
+ }
155
+ receive(line) {
156
+ if (!line.trim()) return;
157
+ let message;
158
+ try {
159
+ message = JSON.parse(line);
160
+ } catch {
161
+ this.fail(/* @__PURE__ */ new Error("Codex discovery returned malformed JSON."));
162
+ return;
163
+ }
164
+ if (!isRecord(message)) {
165
+ this.fail(/* @__PURE__ */ new Error("Codex discovery returned an invalid protocol message."));
166
+ return;
167
+ }
168
+ if (!("id" in message) && typeof message.method === "string") return;
169
+ const pending = this.pending;
170
+ if (!pending || message.id !== pending.id || "result" in message === "error" in message) {
171
+ this.fail(/* @__PURE__ */ new Error("Codex discovery returned an invalid response."));
172
+ return;
173
+ }
174
+ if ("error" in message) {
175
+ this.fail(/* @__PURE__ */ new Error("Codex discovery request failed."));
176
+ return;
177
+ }
178
+ this.pending = void 0;
179
+ pending.resolve(message.result);
180
+ }
181
+ request(method, params) {
182
+ if (this.failure) return Promise.reject(this.failure);
183
+ if (this.pending) return Promise.reject(/* @__PURE__ */ new Error("Codex discovery already has an active request."));
184
+ return new Promise((resolve, reject) => {
185
+ const id = this.nextId++;
186
+ this.pending = {
187
+ id,
188
+ resolve,
189
+ reject
190
+ };
191
+ this.child.stdin.write(`${JSON.stringify({
192
+ id,
193
+ method,
194
+ params
195
+ })}\n`);
196
+ });
197
+ }
198
+ initialized() {
199
+ if (this.failure) throw this.failure;
200
+ this.child.stdin.write(`${JSON.stringify({ method: "initialized" })}\n`);
201
+ }
202
+ assertHealthy() {
203
+ if (this.failure) throw this.failure;
204
+ }
205
+ async close() {
206
+ this.stopping = true;
207
+ clearTimeout(this.deadline);
208
+ this.signal?.removeEventListener("abort", this.onAbort);
209
+ this.child.stdin.destroy();
210
+ this.child.stdout.destroy();
211
+ this.child.stderr.destroy();
212
+ if (!this.hasExited) this.child.kill("SIGTERM");
213
+ const forceKill = setTimeout(() => {
214
+ if (!this.hasExited) this.child.kill("SIGKILL");
215
+ }, 250);
216
+ try {
217
+ await this.exited;
218
+ } finally {
219
+ clearTimeout(forceKill);
220
+ }
221
+ }
222
+ };
223
+ //#endregion
224
+ //#region src/adapters/codex/discovery/index.ts
225
+ const MAX_MODEL_PAGES = 100;
226
+ const MAX_MODELS = 1e4;
227
+ async function discoverCodexInfo(options) {
228
+ const started = Date.now();
229
+ const env = { ...options.env ?? process.env };
230
+ const timeoutMs = options.timeoutMs ?? 3e4;
231
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) throw new Error("Codex discovery deadline must be positive.");
232
+ if (options.signal?.aborted) throw new Error("Codex discovery was cancelled.");
233
+ const executable = resolveDiscoveryExecutable(env);
234
+ const remaining = timeoutMs - (Date.now() - started);
235
+ if (remaining <= 0) throw new Error("Codex discovery timed out.");
236
+ const transport = new DiscoveryTransport(executable, options.cwd, env, remaining, options.signal);
237
+ try {
238
+ const initialized = await transport.request("initialize", {
239
+ clientInfo: {
240
+ name: "cdx_chores",
241
+ title: "cdx-chores",
242
+ version: "1"
243
+ },
244
+ capabilities: { experimentalApi: false }
245
+ });
246
+ if (!isRecord(initialized) || typeof initialized.codexHome !== "string" || !(0, node_path.isAbsolute)(initialized.codexHome) || initialized.codexHome.includes("\0")) throw new Error("Codex discovery did not report a valid absolute Codex home.");
247
+ const version = typeof initialized.userAgent === "string" ? initialized.userAgent.match(/^cdx_chores\/([0-9]+\.[0-9]+\.[0-9]+(?:[-+][\w.-]+)?)(?:\s|$)/)?.[1] : void 0;
248
+ if (!version) throw new Error("Codex discovery did not report a valid CLI version.");
249
+ transport.initialized();
250
+ const configuration = await transport.request("config/read", {
251
+ cwd: options.cwd,
252
+ includeLayers: false
253
+ });
254
+ if (!isRecord(configuration) || !isRecord(configuration.config)) throw new Error("Codex discovery returned invalid configuration metadata.");
255
+ let models = null;
256
+ if (options.view !== "providers") {
257
+ models = [];
258
+ const cursors = /* @__PURE__ */ new Set();
259
+ let cursor;
260
+ for (let page = 0;; page++) {
261
+ if (page >= MAX_MODEL_PAGES) throw new Error("Codex discovery exceeded its model page limit.");
262
+ const response = await transport.request("model/list", {
263
+ includeHidden: false,
264
+ ...cursor === void 0 ? {} : { cursor }
265
+ });
266
+ if (!isRecord(response) || !Array.isArray(response.data) || !(response.nextCursor === null || typeof response.nextCursor === "string")) throw new Error("Codex discovery returned invalid model metadata.");
267
+ if (models.length + response.data.length > MAX_MODELS) throw new Error("Codex discovery exceeded its model count limit.");
268
+ models.push(...response.data);
269
+ if (response.nextCursor === null) break;
270
+ if (!response.nextCursor || cursors.has(response.nextCursor)) throw new Error("Codex discovery returned a repeated or invalid model cursor.");
271
+ cursors.add(response.nextCursor);
272
+ cursor = response.nextCursor;
273
+ }
274
+ }
275
+ transport.assertHealthy();
276
+ return {
277
+ context: {
278
+ cwd: options.cwd,
279
+ codexVersion: version,
280
+ codexHome: initialized.codexHome,
281
+ codexHomeSource: env.CODEX_HOME === void 0 || env.CODEX_HOME === "" ? "default" : "environment"
282
+ },
283
+ config: configuration.config,
284
+ models
285
+ };
286
+ } finally {
287
+ await transport.close();
288
+ }
289
+ }
290
+ //#endregion
291
+ exports.discoverCodexInfo = discoverCodexInfo;