Files
PaperClipAI/server
Ludovic Gasc 78848ae785 fix(server): scrub systemd notify env vars at startup (#22117)
## Thinking Path

> - Paperclip manages AI agents from a control-plane server that runs
under a systemd user service on Linux hosts.
> - Systemd sets `NOTIFY_SOCKET`, `LISTEN_FDS`, `LISTEN_PID`, and
`LISTEN_FDNAMES` in the server environment, and the generated unit uses
`Type=notify` and `NotifyAccess=all`.
> - `#11584` strips those variables inside `runChildProcess`, but
`process.env` in the server process still keeps them.
> - Workspace provisioning commands, plugin workers, external adapters,
and container tools such as `podman run` (`conmon` with default
`--sdnotify=container`) still inherit
`NOTIFY_SOCKET` and can send `STOPPING=1` or `MAINPID=` to systemd.
> - When a child process sends `MAINPID=<child_pid>` and then exits
non-zero, systemd stops the entire `paperclipai.service` cgroup with
`SIGTERM` and interrupts every active run.
> - This pull request captures `NOTIFY_SOCKET` once in
`server/src/services/systemd-notify.ts` and removes the four systemd
variables from `process.env` at startup.
> - The benefit is that only `systemd-notify` invocations from the
server receive `NOTIFY_SOCKET`, while all server descendants remain
isolated from the systemd control channel.

## Linked Issues or Issue Description

Fixes: #14657
Refs: #13608, #11584

## What Changed

- Update `server/src/services/systemd-notify.ts` to capture
`NOTIFY_SOCKET` via `consumeSystemdNotifySocket()` at module load and
delete `NOTIFY_SOCKET`, `LISTEN_FDS`, `LISTEN_PID`,
and `LISTEN_FDNAMES` from `process.env`.
- Pass the captured `NOTIFY_SOCKET` only in the `env` option of
`execFile("systemd-notify", ...)` inside `systemdNotify()`.
- Add unit tests in `server/src/__tests__/systemd-notify.test.ts`
covering startup scrubbing, `systemdNotify` env isolation, missing
`NOTIFY_SOCKET`, and error handling.

## Verification

- Ran `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/systemd-notify.test.ts`: 4 tests passed.
- Ran strict TypeScript check (`tsc --noEmit --strict`) on
`server/src/services/systemd-notify.ts` and
`server/src/__tests__/systemd-notify.test.ts`: 0 errors.
- Confirmed in a running systemd user service that spawned child
processes and agent runs no longer inherit `NOTIFY_SOCKET` while
`systemdNotify(["--ready", ...])` still marks the
service active.

## Risks

- Low risk. The change removes only `NOTIFY_SOCKET`, `LISTEN_FDS`,
`LISTEN_PID`, and `LISTEN_FDNAMES` from the server `process.env` after
capturing `NOTIFY_SOCKET` in memory. Any
caller of `systemdNotify()` still works as before, and any future write
to `process.env.NOTIFY_SOCKET` is consumed on the next call.

## Model Used

- Google Gemini (Antigravity CLI, Orchestrator (Argon)) with tool use
and shell execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-12-05 04:24:00 -08:00
..