always run pi as the service user, drop the sudo -u branch

- the sandbox already drops privileges to the OS user (id -un), so pi-bridge was
  the only code path that switched to a per-user unix account
- the four accounts it targeted (andrepadez, john-wick, fedra, miguelbenoliel)
  are vestigial: created by scripts/provision-existing-users.sh, with no home
  dirs, no files, no processes. the only live account is pastilhas@officer.dev,
  which maps to the service user, so isServiceUser was always true and the
  sudo -u branch could never fire
- add TODO.md tracking the leftover username plumbing, the useradd script, known
  bugs (task-executor relative cwd, bootstrap pi EEXIST), and the ufw 9010 rule

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-15 21:57:49 +00:00
co-authored by Claude Opus 4.8
parent d723fcb23e
commit 3fc7bcb32f
2 changed files with 63 additions and 28 deletions
+10 -28
View File
@@ -12,7 +12,6 @@ import {
getUserExtensionsDir,
getGlobalToolsDir,
getUserToolsDir,
toShellUsername,
} from '../../data-path';
import { logger } from './logger';
@@ -84,7 +83,6 @@ async function resolveApiKeyForModel(model: string): Promise<string | null> {
type SpawnPiOptions = {
sessionFile?: string;
username?: string;
role?: string;
};
@@ -121,43 +119,27 @@ export async function spawnPi(
const homeDir = getHomeDirForRole(email, options?.role ?? null);
const toolsDirs = [getGlobalToolsDir(), getUserToolsDir(email)].join(':');
const shellUsername = options?.username ?? toShellUsername('', email);
const isServiceUser = (options?.username ?? toShellUsername('', email)) === (process.env.USER ?? '');
const env: Record<string, string> = {
HOME: homeDir,
HOME: process.env.HOME ?? '',
OFFICER_USER_HOME: homeDir,
OFFICER_USER_ROOT: join(DATA_PATH, email),
PI_CODING_AGENT_DIR: isServiceUser ? PI_CONFIG_DIR : join(homeDir, '.pi', 'agent'),
PI_CODING_AGENT_DIR: PI_CONFIG_DIR,
PI_TOOLS_DIRS: toolsDirs,
OFFICER_EMAIL_DB: join(DATA_PATH, email, 'emails.db'),
TERM: 'xterm-256color',
PATH: process.env.PATH ?? '',
};
// For service user, keep real HOME so Pi finds its config
if (isServiceUser) {
env.HOME = process.env.HOME ?? '';
}
const proc = Bun.spawn(piArgs, {
cwd,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
env: { ...process.env, ...env },
});
const proc = isServiceUser
? Bun.spawn(piArgs, {
cwd,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
env: { ...process.env, ...env },
})
: Bun.spawn(['sudo', '-u', shellUsername, 'env', ...Object.entries(env).map(([k, v]) => `${k}=${v}`), ...piArgs], {
cwd,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
});
logger.info('Spawned Pi as user', {
username: shellUsername,
logger.info('Spawned Pi', {
model,
skills: skillFlags.filter((f) => f !== '--skill').length,
extensions: extensionFlags.filter((f) => f !== '--extension').length,