Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): share the resolver with CoS and exact-match the Windows env names

[._.]/ Adam Eivy755d8887b03e
diff --git a/server/services/agentWorkspacePrep.js b/server/services/agentWorkspacePrep.js
index d4cdeac825..1da8013f1a 100644
--- a/server/services/agentWorkspacePrep.js
+++ b/server/services/agentWorkspacePrep.js
@@ -34,6 +34,7 @@ import { PATHS } from '../lib/fileUtils.js';
import * as git from './git.js';
import { detectConflicts } from './taskConflict.js';
import { createWorktree } from './worktreeManager.js';
+import { resolveSpawnCwd } from '../lib/spawnCwd.js';
import { getAppWorkspace, getAppDataForTask, createJiraTicketForTask } from './agentPromptBuilder.js';
const ROOT_DIR = PATHS.root;
@@ -67,8 +68,15 @@ export async function prepareAgentWorkspace({ agentId, task }) {
emitLog('error', `❌ ${reason}`, { taskId: task.id });
return { outcome: 'blocked', reason };
}
- if (!existsSync(workspacePath)) {
- const reason = `Workspace path does not exist: ${workspacePath} — fix this app's Repository Path in Apps, then re-run this task.`;
+ // Same resolver the /runs spawn paths use, so a `~/Projects/App` repoPath
+ // expands here too and a repoPath pointing at a FILE is rejected up front
+ // rather than flowing into the git operations below with a non-directory cwd.
+ // Reusing it is what keeps the two validation sites from drifting — an
+ // inline existsSync here did both of those wrong.
+ try {
+ workspacePath = resolveSpawnCwd(workspacePath, ROOT_DIR, `Task ${task.id}`);
+ } catch (err) {
+ const reason = `${err.message} (Task blocked before the agent started, so it could not write into the PortOS directory by mistake.)`;
emitLog('error', `❌ ${reason}`, { taskId: task.id, workspace: workspacePath });
return { outcome: 'blocked', reason };
}
diff --git a/server/services/agentWorkspacePrep.test.js b/server/services/agentWorkspacePrep.test.js
index b0011df56b..974d934ebd 100644
--- a/server/services/agentWorkspacePrep.test.js
+++ b/server/services/agentWorkspacePrep.test.js
@@ -92,6 +92,30 @@ describe('prepareAgentWorkspace — workspace validation (#3180)', () => {
expect(r.reason).toContain('/definitely/not/a/real/repo/path');
});
+ it('blocks when the resolved workspace is a file, not a directory', async () => {
+ const { mkdtempSync, writeFileSync } = await import('fs');
+ const { tmpdir } = await import('os');
+ const { join } = await import('path');
+ const file = join(mkdtempSync(join(tmpdir(), 'prep-')), 'a-file.txt');
+ writeFileSync(file, 'x');
+ getAppWorkspace.mockResolvedValue(file);
+ const task = { id: 't-file', taskType: 'user', metadata: { app: 'primes' } };
+ const r = await prepareAgentWorkspace({ agentId: 'agent-f', task });
+ expect(r.outcome).toBe('blocked');
+ expect(r.reason).toContain('not a directory');
+ });
+
+ // The repoPath field accepts a literal '~/...' — the CoS path must expand it
+ // the same way the /runs path does, or every task for that app is blocked.
+ it('expands a ~ repoPath instead of blocking it', async () => {
+ const { homedir } = await import('os');
+ getAppWorkspace.mockResolvedValue('~');
+ const task = { id: 't-tilde', taskType: 'user', metadata: { app: 'primes', readOnly: true } };
+ const r = await prepareAgentWorkspace({ agentId: 'agent-t', task });
+ expect(r.outcome).toBe('ready');
+ expect(r.workspacePath).toBe(homedir());
+ });
+
it('proceeds when the app resolves to a real directory', async () => {
getAppWorkspace.mockResolvedValue(process.cwd());
const task = { id: 't-ok', taskType: 'user', metadata: { app: 'primes', readOnly: true } };
diff --git a/server/services/shell.js b/server/services/shell.js
index 9de714ccce..621b4fbef5 100644
--- a/server/services/shell.js
+++ b/server/services/shell.js
@@ -38,21 +38,29 @@ const SAFE_ENV_PREFIXES = [
'KUBECONFIG', 'LESS', 'PAGER', 'MANPATH', 'INFOPATH', 'ZDOTDIR', 'STARSHIP_'
];
-// Windows-only additions. The list above is POSIX-shaped: on Windows it drops
-// variables the OS itself needs to create a working process, so a PTY session
-// started from it launches into a crippled shell (no DLL search root, no temp
-// dir, no per-user app data — which is where `claude`/`codex` keep their
-// credentials and config). Only names NOT already covered by a prefix above are
-// listed: PATHEXT/USERPROFILE/HOMEDRIVE/HOMEPATH already match PATH/USER/HOME.
-const SAFE_ENV_PREFIXES_WIN32 = [
+// Windows-only additions, matched as EXACT names rather than prefixes. The list
+// above is POSIX-shaped: on Windows it drops variables the OS itself needs to
+// create a working process, so a PTY session started from it launches into a
+// crippled shell (no DLL search root, no temp dir, no per-user app data — which
+// is where `claude`/`codex` keep their credentials and config).
+//
+// Exact-match is load-bearing, not a style choice: these are individual OS
+// variables, not grouped families like `NPM_`/`XDG_`, so prefix-matching them
+// would admit anything merely STARTING with one — `APPDATA_TOKEN`,
+// `TEMP_SECRET`, and (worst, since it is two characters) `OS_API_KEY` would all
+// be handed to an attachable agent shell by a filter whose entire job is
+// withholding credentials.
+//
+// PATHEXT / USERPROFILE / HOMEDRIVE / HOMEPATH are deliberately absent: the
+// POSIX PATH / USER / HOME prefixes already cover them.
+const SAFE_ENV_NAMES_WIN32 = new Set([
'SYSTEMROOT', 'SYSTEMDRIVE', 'WINDIR', 'COMSPEC',
- 'APPDATA', 'LOCALAPPDATA', 'PROGRAMFILES', 'PROGRAMDATA', 'PROGRAMW6432',
- 'COMMONPROGRAMFILES', 'TEMP', 'TMP',
+ 'APPDATA', 'LOCALAPPDATA',
+ 'PROGRAMFILES', 'PROGRAMFILES(X86)', 'PROGRAMW6432',
+ 'PROGRAMDATA', 'COMMONPROGRAMFILES', 'COMMONPROGRAMFILES(X86)', 'COMMONPROGRAMW6432',
+ 'TEMP', 'TMP',
'NUMBER_OF_PROCESSORS', 'PROCESSOR_ARCHITECTURE', 'OS', 'PSMODULEPATH'
-];
-
-// Precomputed so the merge doesn't run per session.
-const SAFE_ENV_PREFIXES_ALL = [...SAFE_ENV_PREFIXES, ...SAFE_ENV_PREFIXES_WIN32];
+]);
/**
* Filter `process.env` down to the allowlist above.
@@ -78,13 +86,14 @@ const SAFE_ENV_PREFIXES_ALL = [...SAFE_ENV_PREFIXES, ...SAFE_ENV_PREFIXES_WIN32]
*/
export function buildSafeEnv(env = process.env, platform = process.platform) {
const isWin32 = platform === 'win32';
- const prefixes = isWin32 ? SAFE_ENV_PREFIXES_ALL : SAFE_ENV_PREFIXES;
const safeEnv = {};
for (const [key, value] of Object.entries(env)) {
+ // Windows env names are case-insensitive, so both the prefix test and the
+ // exact-name lookup compare against the upper-cased key there.
const candidate = isWin32 ? key.toUpperCase() : key;
- if (prefixes.some(prefix => candidate.startsWith(prefix))) {
- safeEnv[key] = value;
- }
+ const allowed = SAFE_ENV_PREFIXES.some(prefix => candidate.startsWith(prefix))
+ || (isWin32 && SAFE_ENV_NAMES_WIN32.has(candidate));
+ if (allowed) safeEnv[key] = value;
}
return safeEnv;
}
diff --git a/server/services/shell.test.js b/server/services/shell.test.js
index becc05924e..6f75dc4da6 100644
--- a/server/services/shell.test.js
+++ b/server/services/shell.test.js
@@ -649,3 +649,38 @@ describe('buildSafeEnv — POSIX case sensitivity', () => {
expect(safe).not.toHaveProperty('npm_config_mysecret');
});
});
+
+describe('buildSafeEnv — Windows names are exact, not prefixes', () => {
+ // The Windows additions are individual OS variables, not grouped families
+ // like NPM_/XDG_. Prefix-matching them would hand anything merely STARTING
+ // with one to an attachable agent shell — and `OS` is two characters, so it
+ // would admit OS_API_KEY from a filter whose whole job is withholding
+ // credentials.
+ it('does not admit token-like lookalikes of the Windows names', () => {
+ const safe = shell.buildSafeEnv({
+ Path: 'C:\\Windows',
+ APPDATA: 'C:\\Users\\Example\\AppData\\Roaming',
+ APPDATA_TOKEN: 'leak-me',
+ TEMP: 'C:\\Temp',
+ TEMP_SECRET: 'leak-me',
+ OS: 'Windows_NT',
+ OS_API_KEY: 'leak-me',
+ }, 'win32');
+ expect(safe).toHaveProperty('Path');
+ expect(safe).toHaveProperty('APPDATA');
+ expect(safe).toHaveProperty('TEMP');
+ expect(safe).toHaveProperty('OS');
+ expect(safe).not.toHaveProperty('APPDATA_TOKEN');
+ expect(safe).not.toHaveProperty('TEMP_SECRET');
+ expect(safe).not.toHaveProperty('OS_API_KEY');
+ });
+
+ it('keeps the parenthesized ProgramFiles variants that exact-matching could drop', () => {
+ const safe = shell.buildSafeEnv({
+ 'ProgramFiles(x86)': 'C:\\Program Files (x86)',
+ ProgramW6432: 'C:\\Program Files',
+ }, 'win32');
+ expect(safe).toHaveProperty('ProgramFiles(x86)');
+ expect(safe).toHaveProperty('ProgramW6432');
+ });
+});