Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(upgrade): after an unlisted interpreter flag's value the entry is the next path, so a live server started with --dns-result-order is still restarted (#8723 review round 3)

garrytand3f0e0d1fcd7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f70d4ff914..7d84431f25 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,7 +16,7 @@ identifiers and attribution are available in the pre-removal Git revision
### Itemized changes
-- `src/core/upgrade-restart.ts`: `gbrainEntry` resolves a relative entry point against the process's working directory (`/proc/<pid>/cwd` on Linux), skips interpreter flags that take a value (`--preload x`, `--cwd x`, `-r x`) and treats `src/cli.ts` as gbrain when the package two levels up is named gbrain; an unreadable package falls back to the old path match. `bun run <file>` runs the file in its own process and is classified; `bunx`, `bun x` and `bun run <bin>` are wrappers whose child is the gbrain process, so they are no longer counted twice. A worker whose parent is not recognised as gbrain is refused with that parent's pid and executable name; its command line, which can carry credentials, is never printed or put in `--json` output. On macOS, where the working directory is not read, a relative entry still matches a gbrain path; otherwise its worker is refused with the parent's pid and executable name (`ps -p <pid> -o command=` shows the rest).
+- `src/core/upgrade-restart.ts`: `gbrainEntry` resolves a relative entry point against the process's working directory (`/proc/<pid>/cwd` on Linux), skips interpreter flags that take a value (`--preload x`, `--cwd x`, `-r x`; after the value of any other flag, such as `--dns-result-order ipv4first`, the entry is the next path) and treats `src/cli.ts` as gbrain when the package two levels up is named gbrain; an unreadable package falls back to the old path match. `bun run <file>` runs the file in its own process and is classified; `bunx`, `bun x` and `bun run <bin>` are wrappers whose child is the gbrain process, so they are no longer counted twice. A worker whose parent is not recognised as gbrain is refused with that parent's pid and executable name; its command line, which can carry credentials, is never printed or put in `--json` output. On macOS, where the working directory is not read, a relative entry still matches a gbrain path; otherwise its worker is refused with the parent's pid and executable name (`ps -p <pid> -o command=` shows the rest).
- `scripts/eval-spend-guard.sh` (#8717): the lease's start time is read before the signal traps are armed, so no command substitution is parsed between arming them and `wait`. A TERM that landed there ran its trap while bash 5.2 still expected the lease line's `)`, the trap failed with "unexpected EOF while looking for matching `)'", the signal was lost and the guard waited out its command and exited 0. Under CPU load the original guard did that in 3 of 3000 forced TERMs and the fixed guard in 0 of 3000, interleaved. `test/eval-spend-guard.test.ts` pins that no command substitution sits in that window; the exit-143 assertion is unchanged.
- `.github/workflows/test.yml`, `e2e.yml`, `stress.yml` (#8700): the 15 `pglite-snapshot` cache keys hash `src/core/**/*.ts` (plus the snapshot builder and its test helper) instead of `src/core/*.ts` and a hand list of subdirectory files, so a schema input in a new `src/core` folder needs no workflow edit. A change anywhere under `src/core` now rebuilds the snapshot cache.
- `scripts/stress/run.ts` (#8702): a pooler-gated test is exempt from the stress gate only when the workflow job that names it, or the script lines beside it, set `GBRAIN_CI_REQUIRE_PGBOUNCER`; a flag elsewhere in the same file no longer counts. `test/e2e/graduation-cli-topologies.test.ts` is no longer exempt, and a script owner is reported as an "owning pooled runner".
diff --git a/src/core/upgrade-restart.ts b/src/core/upgrade-restart.ts
index 3a956b66b4..a674d1ef88 100644
--- a/src/core/upgrade-restart.ts
+++ b/src/core/upgrade-restart.ts
@@ -99,7 +99,8 @@ function isGbrainCli(path: string, packageName: EntryLookup['packageName']): boo
/**
* The gbrain entry point of a command line and the arguments after it, or null when the command is not gbrain
* itself: a wrapper that starts gbrain as its child (tini, a shell, `timeout`, `bunx`, `bun x`, `bun run <bin>`)
- * is not the gbrain process, while `bun run <file>` runs the file in its own process. A relative entry is
+ * is not the gbrain process, while `bun run <file>` runs the file in its own process. The value of an interpreter
+ * flag the walk does not list is skipped: after it, the entry is the next path. A relative entry is
* returned resolved against the process's working directory; when that directory is unknown it stays relative and
* only a gbrain path matches.
*/
@@ -112,7 +113,10 @@ export function gbrainEntry(command: string, lookup: EntryLookup = {}): { entry:
if (basename(tokens[0]!) === 'bun') {
if (tokens[at] === 'x') return null;
if (tokens[at] === 'run') for (at++; tokens[at]?.startsWith('-'); at++) if (VALUE_FLAGS.has(tokens[at]!)) at++;
- if (tokens[at] !== undefined && !isPath(tokens[at]!)) return null;
+ }
+ if (tokens[at] !== undefined && !isPath(tokens[at]!)) {
+ if (tokens[at - 1]!.startsWith('-')) at = tokens.findIndex((token, i) => i > at && isPath(token));
+ else if (basename(tokens[0]!) === 'bun') return null;
}
}
const token = tokens[at];
diff --git a/test/upgrade-restart.test.ts b/test/upgrade-restart.test.ts
index a4e821b499..1fac32c846 100644
--- a/test/upgrade-restart.test.ts
+++ b/test/upgrade-restart.test.ts
@@ -192,6 +192,20 @@ describe('collectHostProcesses', () => {
]);
});
+ test('an interpreter flag whose value the walk does not know is skipped: the entry is the next path (#8727 review)', async () => {
+ const serve = 'bun --dns-result-order ipv4first /workspace/gbrain/src/cli.ts serve --http';
+ expect(classifyGbrainCommand(serve)).toBe('serve');
+ expect(classifyGbrainCommand('bun --define X=1 /opt/gbrain/src/cli.ts serve')).toBe('mcp');
+ expect(classifyGbrainCommand('node --stack-size 4000 /home/alice-example/.bun/bin/gbrain jobs work')).toBe('worker');
+ expect(gbrainEntry('bun --dns-result-order ipv4first /workspace/acme-example/src/cli.ts serve', { packageName: () => 'acme-example' })).toBeNull();
+ expect(gbrainEntry('bun run gbrain serve')).toBeNull();
+ const processes = await collectHostProcesses(sources({
+ processTable: () => [{ pid: 61, ppid: 1, command: serve, started_ms: 500, gbrain_home: HOME }],
+ heartbeats: async () => [{ pid: 61, kind: 'serve', version: OLD }],
+ }));
+ expect(processes).toEqual([{ pid: 61, ppid: null, role: 'serve', version: OLD, started_ms: 500, ancestors: [], args: ['serve', '--http'], install_version: null, install_root: null }]);
+ });
+
test('a record whose pid the process table does not list as gbrain is stale and dropped (a dead or reused pid is never signalled)', async () => {
const processes = await collectHostProcesses(sources({
processTable: () => [{ pid: 77, ppid: 1, command: 'sleep 600', started_ms: 500, gbrain_home: HOME }],