Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Read ps under LC_TIME=C so the lstart column keeps its documented shape (#54)

Kamil Mazurek544399ac7af3
diff --git a/cli/hook/notify.mjs b/cli/hook/notify.mjs
index 7bdca8d3d..f13acde66 100644
--- a/cli/hook/notify.mjs
+++ b/cli/hook/notify.mjs
@@ -372,7 +372,7 @@ function execFileText(cmd, args, timeout, env) {
}
/**
- * Mirrors PS_ENV in src/lib/tmux.ts. Linux procps substitutes `?` for every byte it cannot print in the
+ * Mirrors ensureUtf8Locale in src/lib/childLocale.ts. Linux procps substitutes `?` for every byte it cannot print in the
* current locale, and a hook child launched by an engine under systemd/docker/ssh usually has no locale
* at all. Measured on Ubuntu 24.04 + procps-ng 4.0.4: `⌘ <title>` reads back as `??? <title>` in BOTH
* comm and args, which kills both halves of the Command Code marker in processMatchScore. macOS never
@@ -392,6 +392,20 @@ function ensureUtf8Locale(env = process.env) {
}
ensureUtf8Locale()
+/**
+ * Mirrors psEnv in src/lib/childLocale.ts. `processRows` below anchors on the `lstart` column, whose
+ * shape belongs to LC_TIME, not to `ps`: only C and en_US produce `DOW MON DD HH:MM:SS YYYY`. en_GB and
+ * en_AU swap day and month, de_DE/fr_FR do that and add dots, ja_JP prints `火 9/15`, ru_RU puts the
+ * year before the time. Every one of those parses ZERO rows, the hook concludes the machine has no
+ * processes, and the engine it was launched by goes unrecognised. LC_ALL has to be cleared because it
+ * outranks both categories; LC_CTYPE stays UTF-8 so Linux procps keeps returning raw bytes.
+ */
+function psEnv(env = process.env) {
+ const configured = env.LC_ALL || env.LC_CTYPE || env.LANG
+ const ctype = configured && /utf-?8/i.test(configured) ? configured : 'C.UTF-8'
+ return { ...env, LC_ALL: '', LC_CTYPE: ctype, LC_TIME: 'C' }
+}
+
/** Second line of defence: /proc is raw bytes, so a glibc without C.UTF-8 still resolves correctly. */
function repairMangledRows(rows) {
if (process.platform !== 'linux') return rows
@@ -560,7 +574,7 @@ function processMatchScore(row, engine, ownership, allowAgentHint = false) {
}
async function processRows() {
- const stdout = await execFileText('ps', ['-axo', 'pid=,ppid=,comm=,lstart=,args='], 3000)
+ const stdout = await execFileText('ps', ['-axo', 'pid=,ppid=,comm=,lstart=,args='], 3000, psEnv())
if (stdout === null) return null
const rows = []
for (const line of stdout.split('\n')) {
diff --git a/cli/src/lib/childLocale.spec.ts b/cli/src/lib/childLocale.spec.ts
index 87b4d149a..6a1b7c618 100644
--- a/cli/src/lib/childLocale.spec.ts
+++ b/cli/src/lib/childLocale.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { ensureUtf8Locale } from './childLocale.js'
+import { ensureUtf8Locale, psEnv } from './childLocale.js'
const supported = process.platform === 'linux' || process.platform === 'darwin'
@@ -40,3 +40,33 @@ describe('ensureUtf8Locale', () => {
expect(env.LC_ALL).toBeUndefined()
})
})
+
+describe('psEnv', () => {
+ /**
+ * The bug this exists for, measured on macOS 15.5 with LANG=en_US.UTF-8 and LC_TIME=en_AU.UTF-8 (macOS
+ * keeps language and region separate, so the split is a two-click setting): `ps -axo lstart=` prints
+ * `Tue 15 Sep 23:54:57 2026`, parseProcessRow accepts 0 of 594 rows, processRows returns `[]` rather
+ * than null, and every pane probe reports no engine — for ten minutes, then START_TIMEOUT.
+ */
+ it('pins LC_TIME to C so lstart keeps the shape parseProcessRow documents', () => {
+ expect(psEnv({ LANG: 'en_US.UTF-8', LC_TIME: 'en_AU.UTF-8' }).LC_TIME).toBe('C')
+ })
+
+ it('clears the LC_ALL that would otherwise outrank LC_TIME', () => {
+ expect(psEnv({ LC_ALL: 'de_DE.UTF-8' }).LC_ALL).toBe('')
+ })
+
+ /** LC_CTYPE is the half ensureUtf8Locale protects: lose it and Linux procps returns `?` for `⌘`. */
+ it('keeps a configured UTF-8 locale as LC_CTYPE, and supplies one when there is none', () => {
+ expect(psEnv({ LC_ALL: 'de_DE.UTF-8' }).LC_CTYPE).toBe('de_DE.UTF-8')
+ expect(psEnv({ LANG: 'fr_FR.utf8' }).LC_CTYPE).toBe('fr_FR.utf8')
+ expect(psEnv({}).LC_CTYPE).toBe('C.UTF-8')
+ expect(psEnv({ LANG: 'POSIX' }).LC_CTYPE).toBe('C.UTF-8')
+ })
+
+ it('returns a copy — the daemon\'s own locale is not rewritten', () => {
+ const env: NodeJS.ProcessEnv = { LANG: 'en_US.UTF-8', LC_TIME: 'en_AU.UTF-8' }
+ psEnv(env)
+ expect(env.LC_TIME).toBe('en_AU.UTF-8')
+ })
+})
diff --git a/cli/src/lib/childLocale.ts b/cli/src/lib/childLocale.ts
index 434ef6733..f032eefb9 100644
--- a/cli/src/lib/childLocale.ts
+++ b/cli/src/lib/childLocale.ts
@@ -26,3 +26,44 @@ export function ensureUtf8Locale(env: NodeJS.ProcessEnv = process.env): void {
if (configured && /utf-?8/i.test(configured)) return
env.LC_ALL = 'C.UTF-8'
}
+
+/**
+ * The environment for every `ps` call whose output is parsed.
+ *
+ * `parseProcessRow` anchors on the `lstart` column because it is the one field with a fixed shape —
+ * but that shape is `LC_TIME`'s, not `ps`'s, and only the C and English-US locales produce the
+ * `DOW MON DD HH:MM:SS YYYY` the parser expects. Measured on macOS 15.5, same machine, one `ps -axo
+ * pid=,ppid=,comm=,lstart=,args=` per locale, counting rows `parseProcessRow` accepts:
+ *
+ * C, en_US 592/592 Tue Sep 15 23:54:57 2026
+ * en_GB, en_AU 0/594 Tue 15 Sep 23:54:57 2026 day and month swapped
+ * de_DE, nl_NL 0/592 Di. 15 Sep. 23:54:57 2026 ...and abbreviations carry dots
+ * fr_FR, es_ES 0/592 mar. 15 sept. 23:54:57 2026
+ * ja_JP, zh_CN 0/596 火 9/15 23:54:57 2026 numeric month, no day name
+ * ru_RU 0/593 вторник, 15 сентября 2026 г. 23:54:57 year before the time
+ *
+ * Zero rows is not a degraded table, it is no table at all, and `processRows` returns `[]` rather than
+ * `null` for it — "we looked and nothing is there" — so every caller believes the machine is empty.
+ * `resolvePaneEngineProcess` then finds no engine under any pane and `watchCreatedPane` spends its full
+ * ten minutes before reporting `START_TIMEOUT`: "<engine> did not expose an engine process within 10
+ * minutes", on a pane where the engine is running and answering hooks. Reported against 1.1.32 / CLI
+ * 0.2.35 on a Mac with LANG=en_US.UTF-8 and LC_TIME=en_AU.UTF-8 (macOS keeps language and region
+ * separate, so this split is a two-click setting, not an exotic one). 13 agents in one session.
+ *
+ * `ensureUtf8Locale` does not cover it: it returns early whenever a UTF-8 locale is already configured,
+ * which on a desktop macOS is always, and it only ever sets `LC_CTYPE`'s category anyway.
+ *
+ * Widening the parser instead was rejected — `вторник, 15 сентября 2026 г. 23:54:57` and `火 9/15` are
+ * not the same grammar with different words, and a regex loose enough for both stops pinning the field
+ * boundaries that `comm`-with-spaces needs it to pin.
+ *
+ * Both categories have to be set explicitly, and `LC_ALL` cleared, because `LC_ALL` outranks both:
+ * - `LC_TIME=C` gives the parser the shape it documents.
+ * - `LC_CTYPE` stays UTF-8 so Linux procps keeps returning raw bytes rather than `?` for every
+ * character it cannot print (see `ensureUtf8Locale` and `repairMangledRows`).
+ */
+export function psEnv(env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
+ const configured = env.LC_ALL || env.LC_CTYPE || env.LANG
+ const ctype = configured && /utf-?8/i.test(configured) ? configured : 'C.UTF-8'
+ return { ...env, LC_ALL: '', LC_CTYPE: ctype, LC_TIME: 'C' }
+}
diff --git a/cli/src/lib/tmux.spec.ts b/cli/src/lib/tmux.spec.ts
index f319d5901..d3b556927 100644
--- a/cli/src/lib/tmux.spec.ts
+++ b/cli/src/lib/tmux.spec.ts
@@ -9,6 +9,7 @@ import {
bypassPermissionActive,
engineProcessMatch,
engineProcessMatchScore,
+ LSTART_MARKER_RE,
parseProcessRow,
resumeSessionId,
sendLiteralToTmux,
@@ -73,6 +74,54 @@ describe('tmux process primitives', () => {
* the live pane. processRows() now reads ps under LC_ALL=C.UTF-8 and repairs any surviving `?` row from
* /proc, which is raw bytes. These two cases pin the before and the after.
*/
+ /**
+ * `lstart`'s shape belongs to LC_TIME, not to `ps`. Captured with
+ * `LC_TIME=<locale> ps -axo pid=,ppid=,comm=,lstart=,args=` on macOS 15.5, one run per locale on the
+ * same machine in the same second. Of sixteen locales tried only C, en_US and hu_HU parse at all; the
+ * rest reorder the fields and yield ZERO rows out of ~590.
+ *
+ * Zero rows is the whole bug: processRows returns `[]`, not null, so "we looked and the machine is
+ * empty" is indistinguishable from the truth, resolvePaneEngineProcess finds no engine under any pane,
+ * and watchCreatedPane burns its full ten minutes before reporting START_TIMEOUT — "claude did not
+ * expose an engine process within 10 minutes" — against a pane where claude is running and still
+ * firing hooks. This is why processRows spawns ps under psEnv instead of inheriting the locale.
+ */
+ it('cannot parse an lstart column written by a locale that reorders it', () => {
+ const rows = [
+ '15160 2281 /Users/admin/.lo Tue 15 Sep 23:10:38 2026 /Users/admin/.local/bin/claude',
+ '15160 2281 /Users/admin/.lo Di. 15 Sep. 23:10:38 2026 /Users/admin/.local/bin/claude',
+ '15160 2281 /Users/admin/.lo mar. 15 sept. 23:10:38 2026 /Users/admin/.local/bin/claude',
+ '15160 2281 /Users/admin/.lo \u706b 9/15 23:10:38 2026 /Users/admin/.local/bin/claude',
+ '15160 2281 /Users/admin/.lo \u0432\u0442\u043e\u0440\u043d\u0438\u043a, 15 \u0441\u0435\u043d\u0442\u044f\u0431\u0440\u044f 2026 \u0433. 23:10:38 /Users/admin/.local/bin/claude',
+ ]
+ for (const row of rows) expect(parseProcessRow(row)).toBeNull()
+
+ // The same process, same second, under the LC_TIME=C that psEnv guarantees.
+ expect(parseProcessRow('15160 2281 /Users/admin/.lo Tue Sep 15 23:10:38 2026 /Users/admin/.local/bin/claude'))
+ .toEqual({
+ pid: 15160,
+ parentPid: 2281,
+ executable: '/Users/admin/.lo',
+ startMarker: 'Tue Sep 15 23:10:38 2026',
+ args: '/Users/admin/.local/bin/claude',
+ })
+ })
+
+ /**
+ * checkSessionRuntime adopts, rather than compares, any saved marker this rejects. It has to reject
+ * localized stamps too: a marker recorded before psEnv landed (hu_HU parsed fine, `K szept. 15 …`) can
+ * never equal the C-locale stamp read back for the same live process, and comparing them would evict a
+ * live pane exactly once per session on upgrade.
+ */
+ it('recognises only a C-locale lstart stamp as a comparable start marker', () => {
+ expect(LSTART_MARKER_RE.test('Tue Sep 15 23:10:38 2026')).toBe(true)
+ expect(LSTART_MARKER_RE.test('Fri Aug 21 09:28:14 2026')).toBe(true)
+
+ expect(LSTART_MARKER_RE.test('K szept. 15 23:10:38 2026')).toBe(false) // hu_HU, parsed pre-fix
+ expect(LSTART_MARKER_RE.test('Tue 15 Sep 23:10:38 2026')).toBe(false) // en_AU
+ expect(LSTART_MARKER_RE.test('Greeting Thu Jul 30 11:00:03 2026')).toBe(false) // pre-fix shifted comm
+ })
+
it('scores Command Code from the real bytes, and cannot from the mangled ones', () => {
const mangled = parseProcessRow(' 185 178 ??? harness-cli Fri Aug 21 09:34:20 2026 ??? harness-cli ubuntu probe')
expect(engineProcessMatchScore(mangled!, 'commandcode')).toBe(0)
diff --git a/cli/src/lib/tmux.ts b/cli/src/lib/tmux.ts
index 02ab226e6..ac44e0e2e 100644
--- a/cli/src/lib/tmux.ts
+++ b/cli/src/lib/tmux.ts
@@ -15,6 +15,7 @@ import {
type AgentCommandOwnershipSnapshot,
} from './engineBin.js'
import { BYPASS_PERMISSION_FLAGS } from './engineLaunch.js'
+import { psEnv } from './childLocale.js'
function cleanPaneTitle(title: string): string | null {
const cleaned = title
@@ -133,7 +134,13 @@ function agentAliasCandidate(row: Pick<ProcessRow, 'executable' | 'args'>): bool
* PID-reuse guard that startMarker exists for.
*
* So anchor on `lstart` instead — it is the one field with a fixed shape (`DOW MON DD HH:MM:SS YYYY`) —
- * and let comm be lazy. The day/month names stay unconstrained so a non-English `LC_TIME` still parses.
+ * and let comm be lazy.
+ *
+ * That shape is only fixed because every `ps` whose output reaches this parser is spawned under
+ * `LC_TIME=C` (`psEnv`, lib/childLocale.ts). The day and month names are left unconstrained as a
+ * courtesy to a locale that merely renames them — NOT as support for one. A locale that REORDERS the
+ * fields parses zero rows here, and most of them do: `Tue 15 Sep` (en_GB, en_AU), `Di. 15 Sep.`
+ * (de_DE), `火 9/15` (ja_JP), `вторник, 15 сентября 2026 г.` (ru_RU). That is what `psEnv` prevents.
*/
export function parseProcessRow(line: string): ProcessRow | null {
const match = /^\s*(\d+)\s+(\d+)\s+(.+?)\s+(\S+\s+\S+\s+\d{1,2}\s+\d{1,2}:\d{2}:\d{2}\s+\d{4})\s*(.*)$/.exec(line)
@@ -175,7 +182,7 @@ function readProcField(pid: number, field: 'cmdline' | 'comm'): string | null {
/** The process table, or null when `ps` itself failed — "we could not look" is not "nothing is there". */
export async function processRows(): Promise<ProcessRow[] | null> {
const rows = await new Promise<ProcessRow[] | null>((resolve) => {
- execFile('ps', ['-axo', 'pid=,ppid=,comm=,lstart=,args='], { timeout: 3000 }, (err, stdout) => {
+ execFile('ps', ['-axo', 'pid=,ppid=,comm=,lstart=,args='], { timeout: 3000, env: psEnv() }, (err, stdout) => {
if (err) { resolve(null); return }
const rows: ProcessRow[] = []
for (const line of stdout.split('\n')) {
@@ -689,8 +696,16 @@ export async function resolvePaneEngineProcess(
return found.ok ? found.identity : null
}
-/** A whole `lstart` stamp — `DOW MON DD HH:MM:SS YYYY` — and nothing else. */
-const LSTART_MARKER_RE = /^\S+\s+\S+\s+\d{1,2}\s+\d{1,2}:\d{2}:\d{2}\s+\d{4}$/
+/**
+ * A whole C-locale `lstart` stamp — `DOW MON DD HH:MM:SS YYYY` — and nothing else.
+ *
+ * The names are spelled out rather than left as `\S+` so this also rejects a stamp recorded while `ps`
+ * still ran under the user's own `LC_TIME` (`K szept. 15 …` parsed fine before `psEnv` landed). Such a
+ * stamp can never equal the C-locale one read back for the same process, so without this it would fail
+ * the identity comparison below and evict a live pane once per session on upgrade.
+ */
+export const LSTART_MARKER_RE =
+ /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{1,2}\s+\d{1,2}:\d{2}:\d{2}\s+\d{4}$/
/** Pane + engine process validation. A saved identity prevents PID reuse from reviving a stale entry. */
export async function validateSessionRuntime(session: RegisteredSession): Promise<boolean> {
@@ -721,8 +736,9 @@ export async function checkSessionRuntime(session: RegisteredSession): Promise<R
if (!found.ok) return { state: found.unknown ? 'unknown' : 'gone', reason: found.reason }
const live = found.identity
const saved = session.processIdentity
- // A persisted identity whose startMarker is not an lstart stamp was written by the pre-fix parser (see
- // parseProcessRow) — its fields are shifted, so it can NEVER match the corrected ones again. Adopt the
+ // A persisted identity whose startMarker is not a C-locale lstart stamp was written either by the
+ // pre-fix parser (see parseProcessRow), with its fields shifted, or by a `ps` that still inherited the
+ // user's LC_TIME (see psEnv). Either way it can NEVER match the corrected ones again. Adopt the
// corrected identity instead of failing: the pane still has a matching engine process, and failing here
// would drop a live session for good (Command Code, the only engine affected, does not re-register on
// its Stop hook). One-time, per session.
diff --git a/cli/src/lib/tmuxAgentDiscovery.ts b/cli/src/lib/tmuxAgentDiscovery.ts
index af81b226f..e53029e00 100644
--- a/cli/src/lib/tmuxAgentDiscovery.ts
+++ b/cli/src/lib/tmuxAgentDiscovery.ts
@@ -18,6 +18,7 @@ import { probeGatewayRuntime } from './gatewayRuntime.js'
import { probeGridAssignment, type GridAssignment } from './gridAssignment.js'
import { probeCodexHome } from './codexHomeProbe.js'
import { isHarnessSession } from './harnessSessionLabel.js'
+import { psEnv } from './childLocale.js'
import type { ProcessIdentity, RegisteredSession } from './registry.js'
import {
ambiguousAgentProcess,
@@ -75,9 +76,14 @@ export interface TmuxAgentDiscoveryDeps {
const MISS_LIMIT = 2
-function execText(command: string, args: string[], timeout: number): Promise<{ ok: true; stdout: string } | { ok: false; error: string }> {
+function execText(
+ command: string,
+ args: string[],
+ timeout: number,
+ env?: NodeJS.ProcessEnv,
+): Promise<{ ok: true; stdout: string } | { ok: false; error: string }> {
return new Promise((resolve) => {
- execFile(command, args, { timeout }, (err, stdout) => {
+ execFile(command, args, { timeout, ...(env && { env }) }, (err, stdout) => {
if (err) { resolve({ ok: false, error: err.message }); return }
resolve({ ok: true, stdout })
})
@@ -238,7 +244,9 @@ export async function probeTmuxAgents(
): Promise<TmuxAgentProbe> {
const [tmux, ps] = await Promise.all([
listTmuxPanes(),
- execText('ps', ['-axo', 'pid=,ppid=,comm=,lstart=,args='], 3_000),
+ // parseProcessRow below anchors on the `lstart` column, which only has its documented shape
+ // under LC_TIME=C — see psEnv (lib/childLocale.ts).
+ execText('ps', ['-axo', 'pid=,ppid=,comm=,lstart=,args='], 3_000, psEnv()),
])
if (!tmux.ok) return { ok: false, error: `tmux list-panes failed: ${tmux.error}` }
if (!ps.ok) return { ok: false, error: `process table failed: ${ps.error}` }