Repository navigation
fix(run): forward wrapper signals, log folded agent keys, opt-in --max-tokens budget #1655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dc52adb
5d074ae
e8a47a7
d8f515f
4285259
1010569
e667dc4
5c36117
b29ff22
432571b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { Schema } from "effect" | ||
| import { ConfigAgentV1 } from "../../src/v1/config/agent" | ||
|
|
||
| const decode = Schema.decodeUnknownSync(ConfigAgentV1.Info) | ||
|
|
||
| describe("ConfigAgentV1 top-level keys", () => { | ||
| test("a known key is accepted as itself", () => { | ||
| const agent = decode({ model: "test/model", steps: 3, description: "Known keys only" }) | ||
| expect(agent).toMatchObject({ model: "test/model", steps: 3, description: "Known keys only" }) | ||
| expect(agent.options).toEqual({}) | ||
| expect(ConfigAgentV1.unknownKeys({ model: "test/model", steps: 3 })).toEqual([]) | ||
| }) | ||
|
|
||
| test("an unknown top-level key is folded into options and can be named", () => { | ||
| const raw = { model: "test/model", budget: 5, audience: "executive" } | ||
| const agent = decode(raw) | ||
| expect(agent.options).toEqual({ budget: 5, audience: "executive" }) | ||
| expect(agent).not.toHaveProperty("steps") | ||
| expect(ConfigAgentV1.unknownKeys(raw)).toEqual(["budget", "audience"]) | ||
| }) | ||
|
|
||
| test("keys nested under options keep working and are not reported", () => { | ||
| const raw = { model: "test/model", options: { audience: "executive", reasoningEffort: "high" } } | ||
| const agent = decode(raw) | ||
| expect(agent.options).toEqual({ audience: "executive", reasoningEffort: "high" }) | ||
| expect(ConfigAgentV1.unknownKeys(raw)).toEqual([]) | ||
| }) | ||
|
|
||
| test("the known-key list is the schema's own", () => { | ||
| expect([...ConfigAgentV1.knownKeys].sort()).toEqual( | ||
| [ | ||
| "name", | ||
| "model", | ||
| "variant", | ||
| "prompt", | ||
| "description", | ||
| "temperature", | ||
| "top_p", | ||
| "mode", | ||
| "hidden", | ||
| "color", | ||
| "steps", | ||
| "maxSteps", | ||
| "options", | ||
| "permission", | ||
| "disable", | ||
| "tools", | ||
| ].sort(), | ||
| ) | ||
| expect(ConfigAgentV1.unknownKeys(undefined)).toEqual([]) | ||
| expect(ConfigAgentV1.unknownKeys(["budget"])).toEqual([]) | ||
| }) | ||
|
|
||
| test("every field the schema interprets is decoded as itself, not folded into options", () => { | ||
| const raw = { | ||
| name: "reviewer", | ||
| model: "test/model", | ||
| variant: "high", | ||
| prompt: "Review the change.", | ||
| description: "Reviews changes", | ||
| temperature: 0.2, | ||
| top_p: 0.9, | ||
| mode: "subagent", | ||
| hidden: true, | ||
| color: "#FF5733", | ||
| steps: 3, | ||
| maxSteps: 4, | ||
| options: { audience: "executive" }, | ||
| permission: { edit: "deny" }, | ||
| disable: false, | ||
| tools: { bash: false }, | ||
| } | ||
| expect(ConfigAgentV1.unknownKeys(raw)).toEqual([]) | ||
| expect(decode(raw).options).toEqual({ audience: "executive" }) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,15 @@ function findAllNodeModules(startDir) { | |
| return paths | ||
| } | ||
|
|
||
| function isExecutableFile(file) { | ||
| try { | ||
| fs.accessSync(file, fs.constants.X_OK) | ||
| return fs.statSync(file).isFile() | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function run(target) { | ||
| // Resolve NODE_PATH so the compiled Bun binary can find external packages | ||
| // installed alongside the wrapper (e.g. @altimateai/altimate-core NAPI module). | ||
|
|
@@ -54,37 +63,68 @@ function run(target) { | |
| } | ||
| } catch { | ||
| // realpathSync failed (e.g. target doesn't exist) — continue without | ||
| // NODE_PATH; spawnSync will report the missing binary via result.error. | ||
| // NODE_PATH; spawn will report the missing binary via the "error" event. | ||
| } | ||
|
|
||
| // Become the binary where node can (POSIX, node >= 22.15 / 23.11): one | ||
| // process, so a signal sent to this pid or to its process group reaches the | ||
| // binary exactly once, as if it had been started directly, and the caller | ||
| // sees the binary's own exit status, a signal death included. A failed | ||
| // execve aborts node instead of throwing, so only an executable file is | ||
| // exec'd; anything else falls through to spawn, which reports it as before. | ||
| if (typeof process.execve === "function" && isExecutableFile(target)) { | ||
| process.execve(target, [target, ...process.argv.slice(2)], env) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (not blocking: a broken-install edge; a correct install never hits it). The |
||
| } | ||
|
|
||
| const result = childProcess.spawnSync(target, process.argv.slice(2), { | ||
| // Otherwise spawn asynchronously and forward termination signals: with | ||
| // spawnSync a `kill <wrapper pid>` ended the wrapper alone and left the | ||
| // binary running as an orphan (observed: a run kept going past its budget). | ||
| // A signal sent to the process group then reaches the binary twice, directly | ||
| // and through the forward; `run` takes a repeat of the same signal within a | ||
| // second as the same delivery when this variable says it is forwarded. | ||
| env.ALTIMATE_WRAPPER_FORWARDS_SIGNALS = "1" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: On Windows this marker makes
|
||
| const child = childProcess.spawn(target, process.argv.slice(2), { | ||
| stdio: "inherit", | ||
| env, | ||
| }) | ||
| if (result.error) { | ||
| console.error(result.error.message) | ||
| process.exit(1) | ||
| const forward = (signal) => { | ||
| try { | ||
| child.kill(signal) | ||
| } catch { | ||
| // The child already exited; its "exit" handler below decides the status. | ||
| } | ||
| } | ||
| // A child killed by a signal (e.g. the kernel OOM killer) has no exit status; report it the way | ||
| // a shell would (128 + signal number) instead of exiting 0 as if the run had succeeded. | ||
| if (result.signal) { | ||
| console.error(`altimate-code terminated by ${result.signal}`) | ||
| process.exit(128 + (os.constants.signals[result.signal] ?? 1)) | ||
| // On Windows the console delivers Ctrl-C to every attached process and | ||
| // child.kill terminates outright, so only SIGTERM is forwarded there. Every | ||
| // signal keeps a listener so the wrapper waits for the child and exits with | ||
| // the child's status. | ||
| for (const signal of ["SIGTERM", "SIGINT", "SIGHUP"]) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major: on the spawn path a group-directed SIGTERM now cuts |
||
| process.on(signal, () => { | ||
| if (signal === "SIGTERM" || process.platform !== "win32") forward(signal) | ||
| }) | ||
| } | ||
| const code = typeof result.status === "number" ? result.status : 1 | ||
| process.exit(code) | ||
| child.on("error", (error) => { | ||
| console.error(error.message) | ||
| process.exit(1) | ||
| }) | ||
| child.on("exit", (status, signal) => { | ||
| // A child killed by a signal (e.g. the kernel OOM killer) has no exit status; report it the way | ||
| // a shell would (128 + signal number) instead of exiting 0 as if the run had succeeded. | ||
| if (signal) { | ||
| console.error(`altimate-code terminated by ${signal}`) | ||
| process.exit(128 + (os.constants.signals[signal] ?? 1)) | ||
| } | ||
| const code = typeof status === "number" ? status : 1 | ||
| process.exit(code) | ||
| }) | ||
| } | ||
|
|
||
| // run() returns while the child is still running, so every branch below must | ||
| // be the only one taken: the first match wins, and the fallback search runs | ||
| // only when neither override is present. | ||
| const envPath = process.env.ALTIMATE_CODE_BIN_PATH | ||
| if (envPath) { | ||
| run(envPath) | ||
| } | ||
|
|
||
| // | ||
| const cached = path.join(scriptDir, ".altimate-code") | ||
| if (fs.existsSync(cached)) { | ||
| run(cached) | ||
| } | ||
| const override = envPath || (fs.existsSync(cached) ? cached : undefined) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: The AVX2/musl probe now runs on every launch even when the cached binary is found
|
||
|
|
||
| const platformMap = { | ||
| darwin: "darwin", | ||
|
|
@@ -222,7 +262,7 @@ function findBinary(startDir) { | |
| } | ||
| } | ||
|
|
||
| const resolved = findBinary(scriptDir) | ||
| const resolved = override ?? findBinary(scriptDir) | ||
| if (!resolved) { | ||
| console.error( | ||
| "It seems that your package manager failed to install the right version of the altimate-code CLI for your platform. You can try manually installing " + | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // altimate_change start — fork-only: one delivery per signal when the npm wrapper forwards it | ||
| // Where node has no process.execve (Windows, older node), the npm wrapper spawns | ||
| // the binary and forwards termination signals to it. A signal sent to the | ||
| // process group (GNU `timeout`, `killpg`, tree-kill, a terminal's Ctrl-C) then | ||
| // reaches the binary twice: directly, and again through the forward. `run` | ||
| // reads a second signal as "exit now", so the pair would skip its shutdown | ||
| // flush. The wrapper says it forwards through ALTIMATE_WRAPPER_FORWARDS_SIGNALS; | ||
| // only then is one repeat of the same signal soon after a delivery dropped. | ||
|
|
||
| /** How soon after a delivery a repeat of the same signal is its forwarded copy. */ | ||
| export const REPEAT_WINDOW_MS = 1_000 | ||
|
|
||
| /** True when the npm wrapper forwards termination signals to this process. */ | ||
| export function forwardedByWrapper(env: Record<string, string | undefined> = process.env): boolean { | ||
| return env.ALTIMATE_WRAPPER_FORWARDS_SIGNALS === "1" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This flag is inherited, not set by the wrapper for this process. The exec path copies Prompt for AI agents |
||
| } | ||
|
|
||
| /** | ||
| * Wraps signal handlers so a forwarded copy of a signal is not a second | ||
| * delivery. Without forwarding every signal is delivered, as before. | ||
| */ | ||
| export function coalescer(forwarded: boolean, now: () => number = Date.now) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Use a monotonic clock for signal coalescing. A backward wall-clock adjustment makes Prompt for AI agents |
||
| const last = new Map<string, { at: number; repeated: boolean }>() | ||
| return (signal: string, handle: () => void) => () => { | ||
| if (forwarded) { | ||
| const at = now() | ||
| const previous = last.get(signal) | ||
| if (previous && !previous.repeated && at - previous.at < REPEAT_WINDOW_MS) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major: the 1 s coalescing also swallows real second signals. The window cannot tell a forwarded copy from a second real signal. (1) On the spawn path, two pid-directed signals sent 300 ms apart ( |
||
| previous.repeated = true | ||
| return | ||
| } | ||
| last.set(signal, { at, repeated: false }) | ||
| } | ||
| handle() | ||
| } | ||
| } | ||
|
|
||
| export * as RunSignal from "./run-signal" | ||
| // altimate_change end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: on Windows with Node 22.15 or later the wrapper crashes before it reaches
spawn. Node definesprocess.execveon every platform (lib/internal/bootstrap/node.jsassigns it unconditionally) and throwsERR_FEATURE_UNAVAILABLE_ON_PLATFORMfrom inside it onwin32. On Windowsfs.accessSync(X_OK)behaves likeF_OK, so the installed.exepassesisExecutableFile, and everyaltimate-codeinvocation (the TUI,run,serve, every subcommand) exits 1 with a stack trace. Repro on macOS with a preload that setsprocess.platformtowin32: on Node 26.8.1 and 22.22.2, head printsTypeError [ERR_FEATURE_UNAVAILABLE_ON_PLATFORM]: The feature process.execve is unavailable on the current platformataltimate-code:76, while base runs the binary. No CI job runs this wrapper on Windows, and the test file skips win32. Fix: gate onprocess.platform !== "win32"as well (or wrap the call intryfor the synchronous throw), and add a test that simulates win32 with a preload.