@clagentic/console 1.10.0-beta.1 → 1.10.0-beta.3

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.
Files changed (94) hide show
  1. package/README.md +1 -1
  2. package/bin/cli.js +10 -0
  3. package/lib/activity-clock.js +23 -0
  4. package/lib/agent-dispatch-tool.js +20 -0
  5. package/lib/build-changes.js +79 -0
  6. package/lib/build-identity.js +192 -0
  7. package/lib/build-monitor.js +125 -0
  8. package/lib/build-sha.json +2 -2
  9. package/lib/build-update-offer.js +141 -0
  10. package/lib/build-update-restart.js +142 -0
  11. package/lib/build-update-standing.js +184 -0
  12. package/lib/cli/daemon-launch.js +8 -5
  13. package/lib/cli/ipc-subcommands.js +6 -0
  14. package/lib/cli/session-subcommands.js +122 -0
  15. package/lib/cli-sessions.js +8 -1
  16. package/lib/cli-transcript-title.js +131 -0
  17. package/lib/codex-defaults.js +8 -2
  18. package/lib/daemon-projects.js +19 -0
  19. package/lib/daemon.js +166 -127
  20. package/lib/debug-log.js +17 -0
  21. package/lib/exit-flush.js +44 -0
  22. package/lib/grant-keys.js +179 -0
  23. package/lib/log-field.js +34 -0
  24. package/lib/memory-shed.js +13 -32
  25. package/lib/project-connection.js +14 -0
  26. package/lib/project-external-trigger.js +91 -123
  27. package/lib/project-http.js +2 -0
  28. package/lib/project-loop.js +5 -4
  29. package/lib/project-notifications.js +4 -0
  30. package/lib/project-prompt-responses.js +7 -5
  31. package/lib/project-session-grants.js +73 -0
  32. package/lib/project-sessions.js +55 -68
  33. package/lib/project-user-message.js +13 -25
  34. package/lib/project.js +25 -5
  35. package/lib/prompt-kinds/permission.js +18 -0
  36. package/lib/prompt-label.js +60 -0
  37. package/lib/prompt-registry.js +123 -13
  38. package/lib/prompt-source.js +65 -0
  39. package/lib/public/css/diagnostics.css +30 -0
  40. package/lib/public/css/notifications-center.css +15 -0
  41. package/lib/public/css/overlays.css +18 -0
  42. package/lib/public/css/rewind.css +8 -0
  43. package/lib/public/modules/agent-dispatch-tool.js +9 -0
  44. package/lib/public/modules/app-messages.js +10 -2
  45. package/lib/public/modules/app-notifications.js +9 -1
  46. package/lib/public/modules/app-panels.js +0 -1
  47. package/lib/public/modules/build-update-detail.js +54 -0
  48. package/lib/public/modules/diagnostics.js +126 -20
  49. package/lib/public/modules/prompt-banner.js +12 -0
  50. package/lib/public/modules/prompt-kinds/permission-choices.js +6 -1
  51. package/lib/public/modules/prompt-kinds/permission.js +15 -0
  52. package/lib/public/modules/prompt-label.js +44 -0
  53. package/lib/public/modules/prompts.js +9 -0
  54. package/lib/public/modules/session-grants.js +131 -0
  55. package/lib/public/modules/sidebar-sessions.js +11 -0
  56. package/lib/public/modules/tools.js +14 -2
  57. package/lib/public/modules/vendor-picker.js +2 -2
  58. package/lib/public/modules/vendor-registry.js +11 -2
  59. package/lib/runtime-env.js +1 -1
  60. package/lib/sdk-bridge.js +114 -115
  61. package/lib/sdk-message-processor.js +25 -87
  62. package/lib/sdk-skill-discovery.js +65 -60
  63. package/lib/server.js +15 -2
  64. package/lib/session-ai-title.js +80 -0
  65. package/lib/session-id.js +33 -0
  66. package/lib/session-input-ipc.js +32 -0
  67. package/lib/session-input.js +409 -0
  68. package/lib/session-reclaim.js +284 -0
  69. package/lib/session-resume.js +172 -0
  70. package/lib/sessions.js +31 -21
  71. package/lib/supervisor.js +39 -0
  72. package/lib/turn-initiator.js +108 -0
  73. package/lib/utils.js +9 -47
  74. package/lib/vendor-registry-message.js +1 -0
  75. package/lib/ws-schema.js +9 -4
  76. package/lib/yoke/adapters/claude-vendor.js +0 -1
  77. package/lib/yoke/adapters/claude-worker.js +6 -4
  78. package/lib/yoke/adapters/claude.js +29 -86
  79. package/lib/yoke/adapters/codex-capabilities.js +9 -6
  80. package/lib/yoke/adapters/codex-vendor.js +15 -15
  81. package/lib/yoke/adapters/codex.js +125 -136
  82. package/lib/yoke/auth-checks.js +8 -21
  83. package/lib/yoke/codex-app-server.js +27 -71
  84. package/lib/yoke/codex-binary.js +254 -0
  85. package/lib/yoke/codex-command.js +122 -0
  86. package/lib/yoke/codex-protocol.js +96 -0
  87. package/lib/yoke/codex-version.js +135 -0
  88. package/lib/yoke/index.js +20 -4
  89. package/lib/yoke/install-probe.js +21 -6
  90. package/lib/yoke/interface.js +14 -12
  91. package/lib/yoke/registry.js +41 -4
  92. package/package.json +1 -2
  93. package/scripts/verify-installed-build.js +59 -17
  94. package/lib/lore-attestation.js +0 -73
@@ -0,0 +1,254 @@
1
+ "use strict";
2
+
3
+ // codex-binary.js - finds the Codex the user installed and says whether the
4
+ // console can drive it. The console ships no Codex of its own: a binary is
5
+ // resolved in this order, the first one found is the one used, and it is
6
+ // validated by asking it for its version.
7
+ //
8
+ // 1. CLAGENTIC_CONSOLE_CODEX_PATH (an absolute path the operator chose)
9
+ // 2. `codex` on PATH
10
+ // 3. common install locations
11
+ //
12
+ // The lookup is the daemon account's. Codex is not run under per-user OS
13
+ // isolation (the agent is disabled in that mode), so there is no per-user
14
+ // lookup.
15
+
16
+ var fs = require("fs");
17
+ var path = require("path");
18
+ var versions = require("./codex-version");
19
+ var codexCommand = require("./codex-command");
20
+
21
+ var CODEX_PATH_ENV = "CLAGENTIC_CONSOLE_CODEX_PATH";
22
+ var VERSION_TIMEOUT_MS = 5000;
23
+ var CACHE_TTL_MS = 30000;
24
+ var INSTALL_REASON = "Codex is not installed. Install it with: npm install -g @openai/codex, then use Re-check.";
25
+
26
+ var CODES = Object.freeze({
27
+ absent: "CODEX_ABSENT",
28
+ tooOld: "CODEX_TOO_OLD",
29
+ invalidOverride: "CODEX_OVERRIDE_INVALID",
30
+ unusable: "CODEX_UNUSABLE",
31
+ });
32
+
33
+ function tooOldReason(found) {
34
+ return "Codex " + found + " is too old; Clagentic: Console needs " + versions.MIN_CODEX_VERSION + " or newer";
35
+ }
36
+
37
+ function isExecutableFile(candidate) {
38
+ try {
39
+ if (!fs.statSync(candidate).isFile()) return false;
40
+ fs.accessSync(candidate, fs.constants.X_OK);
41
+ return true;
42
+ } catch (e) {
43
+ // A path that is not there, or not ours to run, is not a candidate.
44
+ if (e && (e.code === "ENOENT" || e.code === "ENOTDIR" || e.code === "EACCES" || e.code === "EPERM")) return false;
45
+ throw e;
46
+ }
47
+ }
48
+
49
+ var DEFAULT_PATHEXT = ".COM;.EXE;.BAT;.CMD";
50
+
51
+ /**
52
+ * The file names `codex` can have: on Windows the PATHEXT extensions, else the
53
+ * bare name. A native executable is listed before a .cmd/.bat shim whatever
54
+ * PATHEXT says, since a shim has to be run through the command interpreter.
55
+ */
56
+ function executableNames(platform, pathext) {
57
+ if (platform !== "win32") return ["codex"];
58
+ var extensions = String(pathext || DEFAULT_PATHEXT).split(";").map(function (e) { return e.trim(); }).filter(Boolean);
59
+ var names = extensions.map(function (e) { return "codex" + e.toLowerCase(); });
60
+ var direct = names.filter(function (n) { return !codexCommand.isShim(n, "win32"); });
61
+ var shims = names.filter(function (n) { return codexCommand.isShim(n, "win32"); });
62
+ return direct.concat(shims, ["codex"]);
63
+ }
64
+
65
+ /**
66
+ * `codex` on a PATH string, first match, without starting a process. On win32
67
+ * the PATHEXT extensions are tried (codex.cmd, codex.exe), as a shell would.
68
+ * @param {string} pathVar
69
+ * @param {function(string): boolean} isExecutable
70
+ * @param {{platform: ?string, pathext: ?string}} [options]
71
+ */
72
+ function findOnPath(pathVar, isExecutable, options) {
73
+ var opts = options || {};
74
+ var platform = opts.platform || process.platform;
75
+ var pathMod = platform === "win32" ? path.win32 : path;
76
+ var names = executableNames(platform, opts.pathext);
77
+ var dirs = String(pathVar || "").split(pathMod.delimiter).filter(Boolean);
78
+ for (var i = 0; i < dirs.length; i++) {
79
+ if (!pathMod.isAbsolute(dirs[i])) continue;
80
+ for (var j = 0; j < names.length; j++) {
81
+ var candidate = pathMod.join(dirs[i], names[j]);
82
+ if (isExecutable(candidate)) return candidate;
83
+ }
84
+ }
85
+ return null;
86
+ }
87
+
88
+ /** The places a global install usually ends up, per-user ones first. */
89
+ function commonLocations(home) {
90
+ var list = [];
91
+ if (home) {
92
+ ["/.npm-global/bin/codex", "/.local/bin/codex", "/.volta/bin/codex", "/.bun/bin/codex", "/bin/codex"].forEach(function (rel) {
93
+ list.push(home + rel);
94
+ });
95
+ }
96
+ return list.concat(["/usr/local/bin/codex", "/usr/bin/codex", "/opt/homebrew/bin/codex"]);
97
+ }
98
+
99
+ /** The lookups, in the daemon's PATH and home. */
100
+ function defaultDeps(env, commandDeps) {
101
+ return {
102
+ isExecutable: isExecutableFile,
103
+ pathLookup: function () {
104
+ return { path: findOnPath(env.PATH || env.Path, isExecutableFile, { pathext: env.PATHEXT }) };
105
+ },
106
+ home: function () {
107
+ return env.HOME || require("os").homedir();
108
+ },
109
+ runVersion: function (binary) {
110
+ var options = { timeout: VERSION_TIMEOUT_MS, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env: env };
111
+ return codexCommand.execCodexSync(binary, ["--version"], options, commandDeps);
112
+ },
113
+ };
114
+ }
115
+
116
+ function failureOf(error) {
117
+ var stderr = error && error.stderr ? String(error.stderr).trim().split("\n")[0] : "";
118
+ return stderr || (error && error.message) || "unknown error";
119
+ }
120
+
121
+ /** Asks one candidate for its version and judges it against the floor. */
122
+ function validate(candidate, source, deps) {
123
+ var text;
124
+ try {
125
+ text = deps.runVersion(candidate);
126
+ } catch (e) {
127
+ return { state: "unusable", code: CODES.unusable, path: candidate, source: source,
128
+ reason: "Codex at " + candidate + " could not report its version: " + failureOf(e) };
129
+ }
130
+ var parsed = versions.parseVersion(String(text));
131
+ if (!parsed) {
132
+ return { state: "unusable", code: CODES.unusable, path: candidate, source: source,
133
+ reason: "Codex at " + candidate + " reported a version that could not be read" };
134
+ }
135
+ if (versions.compareVersions(parsed, versions.MIN_CODEX_VERSION) < 0) {
136
+ return { state: "too-old", code: CODES.tooOld, path: candidate, source: source, version: parsed.text, reason: tooOldReason(parsed.text) };
137
+ }
138
+ return { state: "ready", path: candidate, source: source, version: parsed.text };
139
+ }
140
+
141
+ function resolveOverride(value, deps) {
142
+ var chosen = String(value).trim();
143
+ function invalid(reason) {
144
+ return { state: "invalid-override", code: CODES.invalidOverride, path: chosen, source: "override",
145
+ reason: CODEX_PATH_ENV + " is set to " + chosen + ", but " + reason };
146
+ }
147
+ if (!path.isAbsolute(chosen)) return invalid("it is not an absolute path");
148
+ if (!deps.isExecutable(chosen)) return invalid("no executable file is there");
149
+ var result = validate(chosen, "override", deps);
150
+ if (result.state === "unusable") return invalid("it could not report a version");
151
+ return result;
152
+ }
153
+
154
+ /**
155
+ * Resolves the Codex the console would run.
156
+ * @param {{env: ?object, deps: ?object}} [options]
157
+ * env is the daemon's environment (the override and PATH are read from it)
158
+ * @returns {{state: string, path: ?string, version: ?string, source: ?string, reason: ?string, code: ?string}}
159
+ * state: ready | absent | too-old | invalid-override | unusable
160
+ */
161
+ function resolveCodexBinary(options) {
162
+ var opts = options || {};
163
+ var env = opts.env || process.env;
164
+ var deps = opts.deps || defaultDeps(env, opts.commandDeps);
165
+
166
+ var override = env[CODEX_PATH_ENV];
167
+ if (typeof override === "string" && override.trim()) return resolveOverride(override, deps);
168
+
169
+ var onPath = deps.pathLookup();
170
+ if (onPath.path) return validate(onPath.path, "path", deps);
171
+
172
+ var locations = commonLocations(deps.home());
173
+ for (var i = 0; i < locations.length; i++) {
174
+ if (deps.isExecutable(locations[i])) return validate(locations[i], "location", deps);
175
+ }
176
+ return { state: "absent", code: CODES.absent, reason: INSTALL_REASON };
177
+ }
178
+
179
+ /** The error a caller that cannot go on without a usable Codex throws. */
180
+ function errorFor(result) {
181
+ var err = new Error(result.reason || "Codex is not available");
182
+ err.code = result.code || CODES.unusable;
183
+ err.resolution = result;
184
+ return err;
185
+ }
186
+
187
+ // Resolutions are cached: resolving runs the binary, and every project asks.
188
+ var _cache = new Map();
189
+
190
+ function cacheKey(opts) {
191
+ var env = opts.env || process.env;
192
+ // Every value the lookup reads: the override, PATH under both spellings
193
+ // (win32 uses Path), PATHEXT and the home the common locations hang from.
194
+ return JSON.stringify([env[CODEX_PATH_ENV] || "", env.PATH || "", env.Path || "", env.PATHEXT || "", env.HOME || ""]);
195
+ }
196
+
197
+ /**
198
+ * resolveCodexBinary, remembered for a short time. `fresh` looks again.
199
+ * Tests and callers that inject lookups bypass the cache.
200
+ */
201
+ function resolveCached(options) {
202
+ var opts = options || {};
203
+ if (opts.deps) return resolveCodexBinary(opts);
204
+ var key = cacheKey(opts);
205
+ var hit = _cache.get(key);
206
+ if (!opts.fresh && hit && Date.now() - hit.at < CACHE_TTL_MS) return hit.result;
207
+ var result = resolveCodexBinary(opts);
208
+ _cache.set(key, { at: Date.now(), result: result });
209
+ return result;
210
+ }
211
+
212
+ /** Forgets what was found, so the next resolution looks again. */
213
+ function invalidate() {
214
+ _cache.clear();
215
+ }
216
+
217
+ /** The usable resolution, or the named error. */
218
+ function requireCodex(options) {
219
+ var result = resolveCached(options);
220
+ if (result.state !== "ready") throw errorFor(result);
221
+ return result;
222
+ }
223
+
224
+ /**
225
+ * The resolution as the install probe and the picker report it.
226
+ * @returns {{installed: boolean, detail: {path: ?string, version: ?string, source: ?string, reason: ?string}}}
227
+ */
228
+ function describe(result) {
229
+ return {
230
+ installed: result.state === "ready",
231
+ detail: {
232
+ path: result.path || null,
233
+ version: result.version || null,
234
+ source: result.source || null,
235
+ reason: result.state === "ready" ? null : (result.reason || null),
236
+ },
237
+ };
238
+ }
239
+
240
+ module.exports = {
241
+ CODEX_PATH_ENV: CODEX_PATH_ENV,
242
+ CODES: CODES,
243
+ INSTALL_REASON: INSTALL_REASON,
244
+ commonLocations: commonLocations,
245
+ findOnPath: findOnPath,
246
+ resolveCodexBinary: resolveCodexBinary,
247
+ resolveCached: resolveCached,
248
+ requireCodex: requireCodex,
249
+ errorFor: errorFor,
250
+ describe: describe,
251
+ invalidate: invalidate,
252
+ // Exported so a test can show every value the lookup reads is in the key.
253
+ _test_cacheKey: cacheKey,
254
+ };
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+
3
+ // codex-command.js - the one place a Codex binary is started from. On Windows
4
+ // the npm install leaves a .cmd or .bat shim, and a batch file cannot be run by
5
+ // execFile, execFileSync or spawn without the command interpreter. A native
6
+ // codex.exe is run directly; a shim is run through the interpreter
7
+ // (ComSpec /d /s /c) with every part of the command line quoted for it. The
8
+ // version check and the app-server start both go through here, so a shim that
9
+ // passes the one runs under the other.
10
+
11
+ var childProcess = require("child_process");
12
+
13
+ var SHIM_RE = /\.(cmd|bat)$/i;
14
+ var CMD_META_RE = /([()\][%!^"`<>&|;, *?])/g;
15
+ var FALLBACK_INTERPRETER = "cmd.exe";
16
+
17
+ function isShim(binary, platform) {
18
+ return platform === "win32" && SHIM_RE.test(String(binary));
19
+ }
20
+
21
+ // One argument as cmd.exe will hand it to a batch file: quoted for the program
22
+ // (backslashes before a quote doubled, the quote escaped), then every character
23
+ // the interpreter treats specially escaped with ^, in two passes.
24
+ //
25
+ // Why two passes is right for `cmd.exe /d /s /c "<shim> <args>"`. The command
26
+ // interpreter parses the line twice before the program sees it: once for the
27
+ // /c string, which removes one layer of carets and hands the rest to the batch
28
+ // file as its arguments, and once more when the shim's own last line forwards
29
+ // them with %*, which removes the next layer. One pass would survive the first
30
+ // parse as live, unescaped text, and the quotes that program-level quoting
31
+ // puts around the argument would then toggle quoting inside the batch file's
32
+ // parse; a `\"` in the argument ends that quoting early, leaving an & or | next
33
+ // to it outside any quote. Two passes keep every quote and operator escaped
34
+ // through both parses, and the C runtime's argv rules (backslashes before a
35
+ // quote) then apply to the text left over. CMD_META_RE is this code's chosen
36
+ // set: a superset of the characters the cmd page names as needing ^
37
+ // (& | ( ) < > ^), which also escapes % ! ; , space, the quote, and wildcard
38
+ // and bracket characters. = + ' { } ~ are left unescaped because every
39
+ // argument is wrapped in quotes, so the arguments of a codex invocation
40
+ // (--config key=value) arrive unchanged.
41
+ //
42
+ // References (Microsoft Learn):
43
+ // https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
44
+ // (with /s and /c, the first and last quotes are stripped; special
45
+ // characters are escaped with ^ or quotation marks)
46
+ // https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/call
47
+ // (%* "refers to all the arguments")
48
+ // https://learn.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments
49
+ // (2n backslashes before a quote give n backslashes and the quote
50
+ // delimits; 2n+1 give n backslashes and a literal quote; the caret is not
51
+ // an escape for argv)
52
+ // The second parse of %* inside a .cmd file is NOT documented by Microsoft.
53
+ // The two-pass escaping rests on the general multi-pass model in Raymond Chen,
54
+ // https://devblogs.microsoft.com/oldnewthing/20091029-00/?p=16213, as an
55
+ // assumption. test/cmd-exe-parse-harness.js models that assumption and checks
56
+ // the round trip for every special character. A run on a real Windows host is
57
+ // still owed.
58
+ function quoteForShim(arg) {
59
+ var text = String(arg);
60
+ if (/[\0\r\n]/.test(text)) {
61
+ throw new Error("[YOKE] a command line argument for a Windows shim cannot hold a line break or NUL");
62
+ }
63
+ text = text.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, "$1$1");
64
+ text = '"' + text + '"';
65
+ return text.replace(CMD_META_RE, "^$1").replace(CMD_META_RE, "^$1");
66
+ }
67
+
68
+ function quoteCommandForShim(binary) {
69
+ var text = String(binary);
70
+ if (/[\0\r\n"]/.test(text)) {
71
+ throw new Error("[YOKE] the path of a Windows shim cannot hold a quote, line break or NUL");
72
+ }
73
+ return text.replace(CMD_META_RE, "^$1");
74
+ }
75
+
76
+ /**
77
+ * What to run so that `binary args...` starts, on a given platform.
78
+ * @param {string} binary
79
+ * @param {string[]} args
80
+ * @param {{platform: ?string, comSpec: ?string}} [opts]
81
+ * @returns {{file: string, args: string[], verbatim: boolean}}
82
+ */
83
+ function invocationFor(binary, args, opts) {
84
+ var o = opts || {};
85
+ var platform = o.platform || process.platform;
86
+ var list = args || [];
87
+ if (!isShim(binary, platform)) return { file: binary, args: list.slice(), verbatim: false };
88
+ var line = [quoteCommandForShim(binary)].concat(list.map(quoteForShim)).join(" ");
89
+ var interpreter = o.comSpec || process.env.ComSpec || FALLBACK_INTERPRETER;
90
+ return { file: interpreter, args: ["/d", "/s", "/c", '"' + line + '"'], verbatim: true };
91
+ }
92
+
93
+ function withVerbatim(options, invocation) {
94
+ if (!invocation.verbatim) return options;
95
+ return Object.assign({}, options, { windowsVerbatimArguments: true });
96
+ }
97
+
98
+ /**
99
+ * execFileSync for a Codex binary. deps: { platform, comSpec, execFileSync }
100
+ * are for tests; callers pass none.
101
+ */
102
+ function execCodexSync(binary, args, options, deps) {
103
+ var d = deps || {};
104
+ var run = d.execFileSync || childProcess.execFileSync;
105
+ var inv = invocationFor(binary, args, d);
106
+ return run(inv.file, inv.args, withVerbatim(options, inv));
107
+ }
108
+
109
+ /** spawn for a Codex binary; deps as for execCodexSync, with spawn. */
110
+ function spawnCodex(binary, args, options, deps) {
111
+ var d = deps || {};
112
+ var start = d.spawn || childProcess.spawn;
113
+ var inv = invocationFor(binary, args, d);
114
+ return start(inv.file, inv.args, withVerbatim(options, inv));
115
+ }
116
+
117
+ module.exports = {
118
+ isShim: isShim,
119
+ invocationFor: invocationFor,
120
+ execCodexSync: execCodexSync,
121
+ spawnCodex: spawnCodex,
122
+ };
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ // codex-protocol.js - what the Codex app-server protocol allows at the version
4
+ // that is actually running. The console drives the Codex the user installed, so
5
+ // a wire field or config key that exists only from some version on is sent only
6
+ // when the running version has it. Every decision is a lookup in the table
7
+ // below by version; nothing here or in a caller branches on an agent's name.
8
+ //
9
+ // The minimums come from a protocol comparison across Codex releases that was
10
+ // made from the project's published sources, not from running those releases;
11
+ // each one is checked against the installed Codex after it is merged.
12
+
13
+ var versions = require("./codex-version");
14
+ var turnInitiator = require("../turn-initiator");
15
+
16
+ var FEATURE_MINIMUMS = Object.freeze({
17
+ // Untrusted projects supply no project AGENTS.md from this version on.
18
+ projectTrustGate: "0.150.0",
19
+ // turn/start accepts a free-text turnTrigger.
20
+ turnTrigger: "0.151.0",
21
+ // The plan tool is off unless tools.update_plan.enabled is set.
22
+ updatePlanTool: "0.152.0",
23
+ });
24
+
25
+ /**
26
+ * The protocol features a running Codex has. An unreadable or missing version
27
+ * is taken to be the oldest supported one, never a newer one.
28
+ * @param {?(string|object)} version
29
+ * @returns {{projectTrustGate: boolean, turnTrigger: boolean, updatePlanTool: boolean}}
30
+ */
31
+ function protocolFeatures(version) {
32
+ var v = versions.parseVersion(typeof version === "string" ? version : "") ||
33
+ (version && typeof version === "object" ? version : null) ||
34
+ versions.parseVersion(versions.MIN_CODEX_VERSION);
35
+ var out = {};
36
+ Object.keys(FEATURE_MINIMUMS).forEach(function (name) {
37
+ out[name] = versions.atLeast(v, FEATURE_MINIMUMS[name]);
38
+ });
39
+ return out;
40
+ }
41
+
42
+ // Codex classifies a turn as automated only for this value; any other string,
43
+ // or none, is a user turn.
44
+ var AUTOMATED_TURN_TRIGGER = "automation_heartbeat_scheduled";
45
+
46
+ /**
47
+ * The turnTrigger for a turn. A typed turn, and a turn that names no initiator,
48
+ * carry none. Every other initiator is automated; Codex has no finer
49
+ * distinction, so which kind of automation it was is not carried.
50
+ * @param {?string} initiator a turn-initiator.js INITIATORS value
51
+ * @returns {?string}
52
+ */
53
+ function turnTriggerFor(initiator) {
54
+ if (!turnInitiator.isInitiator(initiator)) return null;
55
+ if (initiator === turnInitiator.INITIATORS.HUMAN) return null;
56
+ return AUTOMATED_TURN_TRIGGER;
57
+ }
58
+
59
+ var DEFAULT_APPROVAL_POLICY = "on-request";
60
+ var APPROVAL_POLICIES = ["untrusted", "on-request", "never"];
61
+
62
+ /**
63
+ * An approval policy value the running app-server accepts. "on-failure" is an
64
+ * alias of "on-request" in current releases and is not a variant of its own, so
65
+ * it is sent as the value it means; a value nobody recognizes asks rather than
66
+ * running unattended.
67
+ * @param {*} value
68
+ * @returns {string}
69
+ */
70
+ function normalizeApprovalPolicy(value) {
71
+ if (value === "on-failure") return DEFAULT_APPROVAL_POLICY;
72
+ return APPROVAL_POLICIES.indexOf(value) !== -1 ? value : DEFAULT_APPROVAL_POLICY;
73
+ }
74
+
75
+ /**
76
+ * The --config overrides the running version needs for the console's features.
77
+ * @param {object} features protocolFeatures()
78
+ * Project trust is never set here: a repository's own Codex configuration can
79
+ * start programs, so trusting a project is the user's decision, made in Codex.
80
+ * @returns {object} nested config, flattened by the app-server client
81
+ */
82
+ function sessionConfig(features) {
83
+ var config = {};
84
+ if (features.updatePlanTool) config.tools = { update_plan: { enabled: true } };
85
+ return config;
86
+ }
87
+
88
+ module.exports = {
89
+ FEATURE_MINIMUMS: FEATURE_MINIMUMS,
90
+ AUTOMATED_TURN_TRIGGER: AUTOMATED_TURN_TRIGGER,
91
+ DEFAULT_APPROVAL_POLICY: DEFAULT_APPROVAL_POLICY,
92
+ protocolFeatures: protocolFeatures,
93
+ turnTriggerFor: turnTriggerFor,
94
+ normalizeApprovalPolicy: normalizeApprovalPolicy,
95
+ sessionConfig: sessionConfig,
96
+ };
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+
3
+ // codex-version.js - reading and comparing Codex versions. The console runs the
4
+ // Codex the user installed, so every protocol decision starts from a version
5
+ // that is read defensively from text the console does not control: a binary's
6
+ // version output and the userAgent string an app-server reports.
7
+
8
+ // The oldest Codex the console drives. Below it the app-server lacks the
9
+ // thread operations the console relies on (thread/revert from this version on).
10
+ var MIN_CODEX_VERSION = "0.148.0";
11
+
12
+ // A whole whitespace-separated token must be the version: a token with junk
13
+ // before or after the numbers ("abc0.150.0", "0.150.0abc") is not one, so text
14
+ // that merely contains digits is never taken for a version.
15
+ var VERSION_RE = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z][0-9A-Za-z.-]*))?$/;
16
+
17
+ /**
18
+ * Reads the first token of a text that is exactly x.y.z[-pre]. Returns null when
19
+ * there is none, so a caller never acts on a version it could not read.
20
+ * @param {*} text
21
+ * @returns {?{major: number, minor: number, patch: number, pre: ?string, text: string}}
22
+ */
23
+ function parseVersion(text) {
24
+ if (typeof text !== "string" || text.length > 4096) return null;
25
+ var tokens = text.split(/\s+/);
26
+ var m = null;
27
+ for (var i = 0; i < tokens.length && !m; i++) m = VERSION_RE.exec(tokens[i]);
28
+ if (!m) return null;
29
+ var major = parseInt(m[1], 10);
30
+ var minor = parseInt(m[2], 10);
31
+ var patch = parseInt(m[3], 10);
32
+ if (![major, minor, patch].every(Number.isSafeInteger)) return null;
33
+ return {
34
+ major: major,
35
+ minor: minor,
36
+ patch: patch,
37
+ pre: m[4] || null,
38
+ text: major + "." + minor + "." + patch + (m[4] ? "-" + m[4] : ""),
39
+ };
40
+ }
41
+
42
+ /**
43
+ * The version the app-server embeds in its userAgent, "<originator>/<version>
44
+ * (<platform details>)". The originator is read as everything before the first
45
+ * slash so a digit in it is never taken for the version.
46
+ * @param {*} userAgent
47
+ * @returns {?object} parseVersion's result
48
+ */
49
+ function versionFromUserAgent(userAgent) {
50
+ if (typeof userAgent !== "string") return null;
51
+ var m = /^[^/\s]*\/(\S+)/.exec(userAgent.trim());
52
+ return m ? parseVersion(m[1]) : null;
53
+ }
54
+
55
+ function isCount(n) {
56
+ return typeof n === "number" && Number.isFinite(n) && Number.isInteger(n) && n >= 0;
57
+ }
58
+
59
+ // A parsed version handed in by a caller is trusted only when every component
60
+ // is a finite non-negative integer; NaN, a negative or a fraction would order
61
+ // wrongly and silently, so it reads as no version at all.
62
+ function asParsed(value) {
63
+ if (value && typeof value === "object") {
64
+ var ok = isCount(value.major) && isCount(value.minor) && isCount(value.patch) &&
65
+ (value.pre === null || value.pre === undefined || typeof value.pre === "string");
66
+ if (!ok) return null;
67
+ // A release has no prerelease whether it is spelled null or left undefined;
68
+ // comparePre compares by identity, so the two must not differ.
69
+ return value.pre === undefined ? Object.assign({}, value, { pre: null }) : value;
70
+ }
71
+ return parseVersion(value);
72
+ }
73
+
74
+ // Digit strings of any length ordered by value without converting them to
75
+ // numbers: fewer digits (after leading zeros) is smaller, equal length by text.
76
+ function compareDigits(a, b) {
77
+ var x = a.replace(/^0+(?=\d)/, "");
78
+ var y = b.replace(/^0+(?=\d)/, "");
79
+ if (x.length !== y.length) return x.length < y.length ? -1 : 1;
80
+ if (x === y) return 0;
81
+ return x < y ? -1 : 1;
82
+ }
83
+
84
+ function comparePre(a, b) {
85
+ // A release outranks its own prerelease; prereleases compare part by part.
86
+ if (a === b) return 0;
87
+ if (a === null) return 1;
88
+ if (b === null) return -1;
89
+ var pa = a.split(".");
90
+ var pb = b.split(".");
91
+ for (var i = 0; i < Math.max(pa.length, pb.length); i++) {
92
+ if (pa[i] === undefined) return -1;
93
+ if (pb[i] === undefined) return 1;
94
+ var na = /^\d+$/.test(pa[i]);
95
+ var nb = /^\d+$/.test(pb[i]);
96
+ if (na && nb) {
97
+ var d = compareDigits(pa[i], pb[i]);
98
+ if (d !== 0) return d;
99
+ } else if (na !== nb) {
100
+ return na ? -1 : 1;
101
+ } else if (pa[i] !== pb[i]) {
102
+ return pa[i] < pb[i] ? -1 : 1;
103
+ }
104
+ }
105
+ return 0;
106
+ }
107
+
108
+ /**
109
+ * -1, 0 or 1. Either side may be text or a parsed version; an unreadable side
110
+ * throws, since an ordering of an unknown version would be a guess.
111
+ */
112
+ function compareVersions(a, b) {
113
+ var va = asParsed(a);
114
+ var vb = asParsed(b);
115
+ if (!va || !vb) throw new Error("[YOKE] cannot compare an unreadable Codex version");
116
+ var keys = ["major", "minor", "patch"];
117
+ for (var i = 0; i < keys.length; i++) {
118
+ if (va[keys[i]] !== vb[keys[i]]) return va[keys[i]] < vb[keys[i]] ? -1 : 1;
119
+ }
120
+ return comparePre(va.pre, vb.pre);
121
+ }
122
+
123
+ /** Whether a version is at least a minimum; false for an unreadable version. */
124
+ function atLeast(version, minimum) {
125
+ var v = asParsed(version);
126
+ return !!v && compareVersions(v, minimum) >= 0;
127
+ }
128
+
129
+ module.exports = {
130
+ MIN_CODEX_VERSION: MIN_CODEX_VERSION,
131
+ parseVersion: parseVersion,
132
+ versionFromUserAgent: versionFromUserAgent,
133
+ compareVersions: compareVersions,
134
+ atLeast: atLeast,
135
+ };
package/lib/yoke/index.js CHANGED
@@ -57,6 +57,11 @@ function isKnownVendor(vendor) {
57
57
  */
58
58
  function createAdapter(opts) {
59
59
  var vendor = (opts && opts.vendor) || registry.ids()[0];
60
+ if (registry.disabledByOsUsers(vendor, opts && opts.osUsers)) {
61
+ var refusal = new Error("[YOKE] " + registry.osUsersReasonFor(vendor));
62
+ refusal.code = "YOKE_OS_USERS_UNSUPPORTED";
63
+ throw refusal;
64
+ }
60
65
  var adapter = registry.get(vendor).createAdapter(opts);
61
66
  iface.validateAdapter(adapter);
62
67
  wrapCreateQuery(adapter, opts && opts.cwd);
@@ -115,9 +120,20 @@ function checkInstalled() {
115
120
  return result;
116
121
  }
117
122
 
123
+ // Installed, and not ruled out by per-user OS isolation. An agent that does not
124
+ // support that mode is never created while it is on, whatever its install state:
125
+ // the cached install check above is not per mode.
126
+ function isUsable(installed, vendorId, osUsers) {
127
+ return !!installed[vendorId] && !registry.disabledByOsUsers(vendorId, osUsers);
128
+ }
129
+
118
130
  function invalidateAuthCache() {
119
131
  _authCache = null;
120
132
  _installedCache = null;
133
+ // A probe may remember what it found (a resolved binary and its version).
134
+ registry.list().forEach(function(vendor) {
135
+ if (typeof vendor.recheck === "function") vendor.recheck();
136
+ });
121
137
  }
122
138
 
123
139
  /**
@@ -141,10 +157,10 @@ function createAdapters(opts) {
141
157
 
142
158
  registry.list().forEach(function(vendor) {
143
159
  auth[vendor.id] = false;
144
- if (!installed[vendor.id]) return;
160
+ if (!isUsable(installed, vendor.id, opts.osUsers)) return;
145
161
  try {
146
162
  if (vendor.perProjectRuntime) {
147
- adapters[vendor.id] = createAdapter({ vendor: vendor.id, cwd: opts.cwd, slug: opts.slug });
163
+ adapters[vendor.id] = createAdapter({ vendor: vendor.id, cwd: opts.cwd, slug: opts.slug, osUsers: !!opts.osUsers });
148
164
  } else {
149
165
  if (!_sharedAdapters[vendor.id]) {
150
166
  _sharedAdapters[vendor.id] = createAdapter({ vendor: vendor.id, cwd: opts.cwd });
@@ -176,10 +192,10 @@ async function lazyCreateAdapter(adapters, vendor, opts, onInit) {
176
192
  // Force re-check since user may have logged in after server start
177
193
  invalidateAuthCache();
178
194
  var installed = checkInstalled();
179
- if (!installed[vendor]) return null;
195
+ if (!isUsable(installed, vendor, opts.osUsers)) return null;
180
196
 
181
197
  try {
182
- var ad = createAdapter({ vendor: vendor, cwd: opts.cwd, slug: opts.slug });
198
+ var ad = createAdapter({ vendor: vendor, cwd: opts.cwd, slug: opts.slug, osUsers: !!opts.osUsers });
183
199
  if (typeof ad.init === "function") {
184
200
  var initResult = await ad.init(opts || {});
185
201
  if (typeof onInit === "function") onInit(initResult);