Skip to content

fix(host-kit): pin ps lstart reads to the C locale so daemon stop finds a live daemon - #4682

Open
pcbeingused333 wants to merge 5 commits into
callstack:mainfrom
pcbeingused333:fix/ps-lstart-locale
Open

pcbeingused333 wants to merge 5 commits into
callstack:mainfrom
pcbeingused333:fix/ps-lstart-locale

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4656

On macOS (and BSD ps), process identity is the pid plus the ps -o lstart= start time, compared as exact strings. ps spells weekday and month names in the caller's locale, so a daemon spawned under LANG=fr_FR.UTF-8 records mar. 17 nov. 19:42:27 2026 while a client under LC_TIME=C reads Tue Nov 17 19:42:27 2026 for the same pid. processStartTimesDiffer then reports two renderings of one start time as different lifetimes, so daemon stop answers stopped: false, deletes daemon.json and the lock, and leaves the daemon alive.

Fix

  • The synchronous single-field, the synchronous batched and the asynchronous ps identity reads in host-process.ts now run with LC_ALL=C (shared psReadOptions in a new ps-start-time.ts, which also keeps host-process.ts under the max-lines limit).
  • processStartTimesDiffer treats a C-form start time against a localized one (both carrying a clock time) as unknown evidence rather than proof of a different lifetime, so records written by an older version in another locale do not orphan daemons that are already running.

Test

Two tests in host-process-liveness.test.ts: every ps read passes env.LC_ALL === 'C', and a localized start time against its C-locale rendering does not differ (while a real one-second difference still does). Both fail on main and pass with the change.

Reproduction

Spawn a daemon with LANG=fr_FR.UTF-8, then run LC_TIME=C agent-device daemon stop --state-dir <dir> --json as in the issue. Not reproduced on a macOS host for this PR; the unit tests pin the ps environment and the comparison instead.

Disclosure

This change was prepared with the help of an AI coding assistant (Claude Code) and submitted by the account holder.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

View guided diff

ps -o lstart prints weekday and month names in the caller's locale, so a
daemon and a client under different LC_TIME compared one start time as
different lifetimes and daemon stop orphaned a live daemon on macOS.
Run the ps identity reads with LC_ALL=C and treat a C-form start time
against a localized one as unknown instead of reassigned.

Fixes callstack#4656
@thymikee

Copy link
Copy Markdown
Member

The locale pin in 70f0b17 is too wide: it also changes how command text is read, and that breaks managed-process ownership for non-ASCII paths.

The pin applies to every host-kit ps read, including the pid=,command= batch read and readProcessField('command='/'state=') (host-process.ts:200). Only lstart needs it. BSD/macOS ps escapes non-ASCII argv bytes under LC_ALL=C: a process started with /Users/mich/é/ü prints that path by default and /Users/mich/M-CM-)/M-CM-< under the pin. listHostProcesses stays unpinned, so one scan now compares UTF-8 command text with escaped text. In agent-browser-process-record.ts:161-162, first.command === last.command && command === first.command compares the pinned read with the unpinned list. A managed Chrome or agent-browser process with a non-ASCII argv path (a non-ASCII home dir or --user-data-dir) fails that filter, so it is never recorded or cleaned up. The command matchers in judgeProcessIdentity (host-process.ts:789-796) and the app-log and recording matchers see the escaped text too. They then either refuse to signal or report the process as reassigned. Before this PR both reads used the same locale and agreed. The rule the code must satisfy: no command text read by host-kit changes encoding between callers, and every ps read whose output contains lstart is pinned. The set of sites is the lstart-bearing reads, meaning HOST_PROCESS_IDENTITY_PS_ARGS (sync and async) and readProcessField when the field is lstart=. Please add a regression that asserts the pid=,command= call carries no LC_ALL pin.

Not blocking, and fine to take or leave: the new test checks the env argument on mocked runCmd calls and expects every call to be pinned, which locks in the problem above, and no test drives judgeProcessIdentity or stopDaemonProcess with a legacy-locale record against a C-locale read (for example mar. 17 nov. 19:42:27 2026 against Tue Nov 17 19:42:27 2026, expecting unverified). simulator-boot.ts already defines C_LOCALE_ENV and a C-form lstart regex, so this PR adds a third spelling; exporting one from host-kit and importing it there would fix that. env: { LC_ALL: 'C' } replaces the whole environment. PATH and HOME do not matter, because ps is resolved by absolute path, but dropping TZ changes how lstart renders, and a record written under a custom TZ would still count as differing. psStartTimesLocaleMismatch only matches C form against non-C form, so two different localized renderings still compare as differing, and the comment should state that legacy-only scope.

Could the fix be smaller? Pin only the lstart reads, keep command and state reads on default options, keep the 'unknown' rule for legacy records, and put the C-form check beside processStartTimesDiffer so simulator-boot.ts can reuse it. Nothing else needs to change first. As an optional follow-up, isAgentDeviceDaemonProcess in daemon-process.ts could use the same comparison, so a legacy-locale daemon can still be stopped after the upgrade.

The Coverage check is failing, but its log ends mid-run in the react-native companion worker tests with no assertion or error line, so it does not point at this PR. Please re-run it. No conflicts. Before merge, narrow the pin to the lstart reads and add the unpinned-command regression. The issue's own reproduction (a French-locale daemon with LC_TIME=C daemon stop) has not been run on macOS yet, so a live run of that route showing daemon stop finding the daemon would close it.

A separate security cross-check of this head, run because process identity decides which process gets signalled, adds two more items before merge:

  • The pin drops the child environment and changes the identity clock (ps-start-time.ts:11). An explicit exec env replaces the inherited one, so ps loses TZ (and LANG) and renders lstart in the host zone. With the same explicit TZ on both sides of an upgrade, the new CLI reads a different start time for the same live PID, and stopDaemonProcess returns already-exited, which retires a live daemon's registration. Apply LC_ALL=C as a final override on top of the effective child environment instead of replacing it.
  • The legacy-locale guard misses real macOS formats (ps-start-time.ts:25). Under nb_NO.UTF-8, macOS ps prints the clock with dots, and other locales omit HH:mm:ss entirely. A record written that way does not match the C-form check, so the two renderings count as different lifetimes and a live daemon is again treated as exited. A mismatch should prove exit only when both values are in the same verified format. Otherwise treat the legacy record as unverified, never as gone. Please add a test with a real localized format that has no colon-separated clock, exercised at the daemon stop boundary.

Signal authorization still requires an exact identity match, so neither item lets the CLI signal the wrong process. Both make it give up on a live daemon, which is the orphaning bug #4656 reports, now coming from another direction.

thymikee and others added 3 commits November 18, 2026 20:31
…tamps unverified

Addresses the blocking review on callstack#4682:

- The LC_ALL=C pin now applies only to ps reads whose output carries
  lstart (HOST_PROCESS_IDENTITY_PS_ARGS sync/async and
  readProcessField('lstart=')). The pid=,command= batch read and
  readProcessField('command='/'state=') keep the caller's locale, so
  command text reads with one encoding from every host-kit caller,
  matching the unpinned listHostProcesses scan. BSD/macOS ps escapes
  non-ASCII argv bytes under LC_ALL=C (/tmp/é-ü -> /tmp/M-CM-)-M-CM-<),
  which broke managed-process ownership for non-ASCII paths.
- The pin is a final override on top of the effective child environment
  (new effectiveChildEnv in exec.ts), so TZ and the rest of the child
  env survive: replacing the whole env dropped TZ and rendered lstart
  in the host zone under the pin (verified live: TZ=Pacific/Kiritimati
  record Thu Nov 19 09:29:21 2026 vs no-TZ read Wed Nov 18 20:29:21).
- processStartTimesDiffer proves a different lifetime only when both
  sides share one verified format (linux: token, C-form lstart, Windows
  invariant stamp). A legacy localized record — with or without a
  colon-separated clock, e.g. mer. 18 nov. 20:29:43 2026 or
  18 nov. 2026 — is unverified: never declared exited at daemon stop,
  never a signal authorization.

Regressions: pin scope + UTF-8 command reads + TZ retention in
host-process-liveness.test.ts, legacy verdicts in host-process.test.ts
and owner-identity*.test.ts, and at the daemon stop boundary in
daemon-process-takeover.test.ts (legacy record -> retained
identity-unverified with the daemon alive; non-ASCII argv reads as
UTF-8). Token-based reuse tests now use verified-format tokens.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
…heck the lstart helpers

Move the start-time format classification and processStartTimesDiffer into
ps-start-time.ts (re-exported through the process barrel) so host-process.ts
stays under 1000 lines, and use non-null match groups in the takeover test's
locale helpers. Also replaces hex escapes with Unicode escapes in the
liveness test.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
…wn-env word

The layering guard (R86 process-globals) forbids reading process.env outside
declared process entries, so the pin cannot be materialized from
process.env in exec.ts. ExecOptions gains envOverrides, which
resolveSpawnEnv applies as the final word over the bound host scope's
environment plus the request env: LC_ALL=C is pinned while TZ and the
rest of the effective child environment survive. Outside a scope an
explicit env still takes the overrides on top, and with neither the
child inherits natively.

The TZ regression moves to exec.test.ts, which spawns a real child:
withCommandHost(host TZ=Pacific/Kiritimati) + envOverrides LC_ALL=C
observes 'Pacific/Kiritimati|C', and the override also wins over a
request-env LC_ALL.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@thymikee

thymikee commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Progress at 7ef7ebb. The locale override is now limited to start-time reads, keeps the effective child environment, and leaves Unicode command reads unpinned. On macOS, a real daemon started under French LC_TIME stopped gracefully from a C-locale client. The worker also verified graceful shutdown at a matching explicit TZ, and verified that legacy localized records return identity-unverified with the process and registration retained.

The cross-time-zone case remains open. The current C-form timestamp has no time-zone provenance. Coverage on this exact head also reports eager-closure growth from ps-start-time.ts; this needs a source fix before merge. The cross-zone run and exact-head checks are still pending.

@thymikee

Copy link
Copy Markdown
Member

This review covers f88ff09. The newer head 7ef7ebb changes how the locale pin reaches the spawn environment and will get its own delta review; the time-zone and evidence items below do not depend on it. At f88ff09, the earlier command pin and environment replacement findings are fixed, but birth identity and live validation still need work. The real dotted-clock fixture and full macOS run remain outstanding.

Could the process owner give birth tokens locale- and time-zone-independent, identifiable provenance? Starting a daemon in Pacific/Kiritimati and stopping it with TZ=UTC produces different C-shaped strings for the same PID, so the comparison can report a live daemon as exited. Conversely, a recycled PID held by a matching daemon can have the same local clock value and pass signal authorization. Both comparisons must treat old unqualified renderings as unknown unless the original lifetime is independently established; keep argv probes separate.

Please remove the redundant non-ASCII assertion and cover this through the environment-sensitive process reader and real-child stop test. It compares the same constructed string on both sides and passes even when the command read still has the reported regression.

The takeover test uses a stub executable and synthetic timestamps, so it does not establish graceful stop or live resource ownership. At the fixed head on macOS, start a real detached daemon under fr_FR.UTF-8 in a fresh state directory, capture its PID and token, and run daemon stop --state-dir

--json under LC_TIME=C; show stopped:true, mode:graceful, and PID exit. Repeat across different TZ values. With a separate pre-fix registration from an actual non-C locale, show typed identity-unverified refusal, a live PID, and retained metadata and lock. Run Apple simulator record start and stop through the daemon and show the recorder stops and releases its resource. If the snapshot change remains, show successive requests across provider locales retain the same live snapshot generation.

Not blocking: these can be taken or left: use a valid 21-digit CIM sample for stale Windows fixtures and remove unreachable platform branches; resolve scoped environment overrides once at spawn; bind an installed UTF-8 locale for the real-child Unicode test; use a recorded nb_NO.UTF-8 dotted-clock value; and keep the existing table case instead of the duplicate one-second reuse assertion.

Could the already loaded process capability own POSIX birth-token construction and its sync and async probe policy, with exec resolving scoped overrides once at spawn and the cancellable snapshot provider reusing that policy? A versioned token could identify comparable clocks while historical raw values remain unknown, preserving Linux /proc and Windows CIM tokens; choosing provenance and the legacy refusal rule at the process boundary comes first, while preserving Apple provider cancellation and routing, removing the extra eager import, and consolidating fixtures. No ADR amendment is needed for that ownership.

Android Smoke Tests passed, but Linux normally uses /proc and does not exercise macOS lstart. Repo Guards reports R86 on the added process.env capture, and Coverage reports ten eager-closure assertions with one extra module each. There are no known conflicts. Before merge, fix birth-token ownership and scope, clear R86 and the eager-closure checks at their source, and provide the exact-head macOS receipts.

…ost-process module

Move the ps-start-time logic into the already-loaded host-process.ts so the
eager-closure budget is unchanged, and give newly produced POSIX birth tokens
explicit comparable provenance: a pinned C-form lstart is parsed in the
reader's zone and recorded as posix:<epochMs>, so identity survives a
cross-TZ stop. Historical raw renderings stay unqualified: they yield
unverified identity and never prove exit or authorize a signal. Linux /proc
and Windows CIM routes, unpinned argv reads, and cancellation are unchanged.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@thymikee

Copy link
Copy Markdown
Member

At 7ef7ebb, the locale pin no longer applies to argv reads, but the process birth identity and SDK startup issues remain from the earlier review.

Could the host-kit observation owner emit one locale- and timezone-independent birth identity with provenance? A daemon registered under Pacific/Kiritimati and stopped under UTC gets different C-shaped lstart strings for the same PID, and the classifier treats the shared shape as a shared clock. The exit wait can report exit before signaling, so detached stop can report not-running and retire a live daemon's lock and metadata. Claim, lock, and managed-process observations also treat the difference as reuse. Old raw POSIX values need to remain unknown unless the lifetime is independently established. Identity classifier

Could every live signal authorization gate require a positive match between comparable birth identities, with command matching independent? A recycled PID can have the same raw local-second string under a different timezone, bypass the mismatch check, and pass the daemon entry-path check. That can authorize a signal to a different process lifetime. Legacy raw POSIX values need to remain unknown for equality too. Signal gate

Could scoped and unscoped launch and observation routes use the same clock, with the captured host bound at declared entrypoints for probes that need its environment? An unscoped lstart probe receives only LC_ALL=C, while Node ensureDaemon passes the captured TZ to the child. With a nondefault TZ, the parent can reject its live child during readiness. The scoped real-child test does not cover this route. Please verify a fresh Node ensureDaemon call with a nondefault TZ. Probe environment

Could the Unicode test remove the self-comparison and retain the environment-sensitive reader regression and real-child stop test? It supplies the same constructed argv to both sides without invoking ps, so it stays green with the earlier overbroad pin. Test

Could the valid and stale process birth fixtures be shared in test-kit? The same values appear in three test files, so each identity format change requires three updates. Fixture

Could you attach exact-head macOS receipts for the CLI and recording routes? The takeover test uses a stub process and synthetic timestamps, and the progress comment reports partial real macOS validation without the underlying transcript. At the fixed SHA, start a detached daemon in a fresh state directory under fr_FR.UTF-8, record its PID and birth identity, and run daemon stop --state-dir

--json under LC_TIME=C; show stopped:true, mode:graceful, and that PID exiting. Repeat with different explicit TZ values, and show a fresh Node ensureDaemon call under a nondefault TZ recognizing its child. For a genuine pre-fix non-C registration, show details.terminationReason=identity-unverified, the live PID, and retained metadata and lock. Through an Apple simulator daemon session, run record start and record stop and show the recorder exiting and its owned resource released. Include the SHA, commands, and outputs. Takeover test

Not blocking: the stale CIM fixtures could use valid distinct 21-digit samples, the Unicode child test could bind an installed UTF-8 locale, the stop regression could use a recorded nb_NO.UTF-8 dotted-clock value, and the redundant one-second reuse test could be removed; please take or leave these suggestions.

Could comparable POSIX identities live in the already loaded host-kit process capability alongside the Linux and Windows producers, with sync and async paths sharing one probe policy and the existing exec resolver owning spawn overrides? Could argv reads stay separate, qualified observations yield positive and negative lifetime judgments, and the extra eager import be collapsed? First, the canonical POSIX clock, provenance discriminator, and unknown legacy behavior for both matches and differences need definitions; saved-state structural equality should remain intact. Apple snapshot provider routing can stay separate, and no ADR amendment is needed.

The Coverage failure most likely comes from the added eager module, since its inbound static imports remain; please confirm from the job log. No conflicts are known. Before merge, please fix birth identity and the unscoped SDK route, repair the regressions and eager closure growth at their source, and provide passing exact-head checks and the macOS receipts above.

@thymikee

Copy link
Copy Markdown
Member

Round 2 at head ea5c816b274e7fb88e0b4fabb441df5c164f86b2 (single fast-forward 7ef7ebb7d4..ea5c816b27, no force-push).

Eager-closure / Coverage failure — fixed at the seam, no baseline changes. The ps-start-time module is deleted and its logic now lives in the already-loaded host-process.ts (984 lines, under the 1000 cap). pnpm exec vitest run --project unit-core scripts/__tests__/eager-closure-budgets.test.ts passes (909/909); the full receipt below shows vitest-related passed and zero eager-closure failures — the Coverage job's "adds 1 module" finding is gone.

Cross-TZ identity — closed with provenanced birth tokens. New POSIX reads canonicalize the pinned C-form lstart (parsed in the reader's zone) to posix:<epochMs>, a zone-independent token with explicit comparable provenance. processStartTimesDiffer proves a different lifetime only between two tokens of the same provenanced format (linux:, posix:, 21-digit Windows CIM); historical raw renderings — localized and raw C-form alike — are unqualified, so they yield identity-unverified: never already-exited, never a signal. isAgentDeviceDaemonProcess requires a provenanced expected token plus exact match plus command match. Linux /proc and Windows CIM routes, unpinned argv reads (UTF-8 command text preserved; only lstart=-bearing reads pin LC_ALL=C via envOverrides), and cancellation are unchanged. No shutdown-transport or signal-scope expansion.

Live macOS verification at exact head ea5c816b27 (disposable daemons, per-process env only, no host-global settings, owner-proven cleanup, no leftover daemons):

  • Cross-locale: daemon started under LC_ALL=fr_FR.UTF-8 recorded processStartTime: "posix:1795036084000"; LC_ALL=C LC_TIME=C agent-device daemon stop --json → stopped: true, mode: "graceful", pid 69017 gone, state dir cleaned.
  • Cross-TZ: daemon started under TZ=Pacific/Kiritimati recorded posix:1795036104000 while raw lstart rendered Thu Nov 19 11:08:24 2026 there vs Wed Nov 18 21:08:24 2026 under TZ=UTC; stop under TZ=UTC LC_ALL=C → stopped: true, mode: "graceful", pid 70294 gone.
  • Legacy record: raw C-form Wed Nov 18 22:08:42 2026 written into a live daemon's daemon.json (pid 71455); LC_ALL=C daemon stop --json → success: false, terminationReason: "identity-unverified", removedInfo: false, daemon.json + daemon.lock retained, pid still alive. Owner cleanup (SIGTERM) confirmed the pid gone and the state dir removed.

Checks receipt: pnpm check:affected --run --head ea5c816b274e7fb88e0b4fabb441df5c164f86b2 → receipt .tmp/validation/run-s0JGzn/receipt.json, exactCommit=true: format, lint, typecheck, layering, fallow, build, vitest-related all passed; unit failed only on 6 load-induced flakes while a concurrent validation ran on this host (release-publish ×4, snapshot-helper-exit-result ×1, macos-helper-package-contents ×1) — all 3 files pass in isolation (40/40), and none are touched by this PR; the pre-existing baseline failures (eager-closure-budgets ×65, daemon-process-takeover) are gone. pnpm format clean, pnpm build green, frozen lockfile install used throughout.

No device proof was run — no device is assigned to this task via T3. Not merged, not watched; readiness labels untouched.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: daemon stop orphans a live daemon when the daemon and the client run under different LC_TIME locales (ps -o lstart is not locale-pinned)

2 participants