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

review: reuse bufferedSpawn for cmd sources + guard cmd branch on repoPath (#2277)

Adam Eivy96705a64e846
diff --git a/server/services/layeredIntelligence.js b/server/services/layeredIntelligence.js
index 4ee486cd88..cbfaaedf1c 100644
--- a/server/services/layeredIntelligence.js
+++ b/server/services/layeredIntelligence.js
@@ -24,6 +24,7 @@ import { join, resolve, relative, isAbsolute } from 'path';
import { readFile, writeFile, appendFile, realpath } from 'fs/promises';
import { existsSync } from 'fs';
import { DAY, tryReadFile, readJSONFile, safeJSONParse, PATHS } from '../lib/fileUtils.js';
+import { bufferedSpawn } from '../lib/bufferedSpawn.js';
// Tracker labels + slug marker. The slug is the stable dedup key the reasoner
// chooses; it is embedded in each filed issue body so a later run (or the
@@ -347,7 +348,7 @@ export async function gatherSources(app, config, { cosPath = PATHS.cos } = {}) {
} else if (custom.type === 'http' && typeof custom.url === 'string') {
const content = await fetchHttpSource(custom.url);
if (content) out[key] = content.slice(0, 8000);
- } else if (custom.type === 'cmd' && typeof custom.cmd === 'string') {
+ } else if (custom.type === 'cmd' && typeof custom.cmd === 'string' && repo) {
const content = await runShellCommand(custom.cmd, { cwd: repo });
if (content) out[key] = content.slice(0, 8000);
}
@@ -386,27 +387,17 @@ export async function fetchHttpSource(url, { timeoutMs = 10_000, fetchImpl = fet
/**
* Run a user-configured shell command for a `cmd` custom source and return its
* stdout. Deterministic read, no LLM. Runs the full command string through the
- * shell (e.g. `git log --oneline -20 | head`), bounded by a wall-clock timeout,
- * in the app's repo dir. Returns null on non-zero exit / no output so a failing
- * command just omits the key. `exec` is injectable for tests.
+ * shell (e.g. `git log --oneline -20 | head`) via the shared `bufferedSpawn`
+ * helper — which caps output, kills the whole process tree on timeout (so a
+ * hung pipeline grandchild can't linger), and never rejects. Returns null on
+ * non-zero exit / timeout / no output so a failing command just omits the key.
+ * `exec` is injectable for tests.
*/
-export async function runShellCommand(cmd, { cwd, timeoutMs = 15_000, exec = runShell } = {}) {
+export async function runShellCommand(cmd, { cwd, timeoutMs = 15_000, exec = bufferedSpawn } = {}) {
if (typeof cmd !== 'string' || !cmd.trim()) return null;
- const { code, stdout } = await exec(cmd, { cwd, timeout: timeoutMs });
+ const { code, stdout } = await exec(cmd, [], { cwd, timeoutMs, shell: true });
if (code !== 0) return null;
- return stdout.trim() || null;
-}
-
-/** Run a full command string through the shell, resolving `{ code, stdout, stderr }` (never rejects). */
-function runShell(cmd, options = {}) {
- return new Promise((resolve) => {
- const child = spawn(cmd, { shell: true, windowsHide: true, ...options });
- let stdout = '', stderr = '';
- child.stdout?.on('data', d => { stdout += d.toString(); });
- child.stderr?.on('data', d => { stderr += d.toString(); });
- child.on('close', code => resolve({ code, stdout, stderr }));
- child.on('error', err => resolve({ code: -1, stdout: '', stderr: err.message }));
- });
+ return (stdout || '').trim() || null;
}
/**
diff --git a/server/services/layeredIntelligence.test.js b/server/services/layeredIntelligence.test.js
index 3a10151dce..f657ff8d28 100644
--- a/server/services/layeredIntelligence.test.js
+++ b/server/services/layeredIntelligence.test.js
@@ -489,15 +489,20 @@ describe('fetchHttpSource', () => {
});
describe('runShellCommand', () => {
- it('returns trimmed stdout on exit 0', async () => {
+ it('returns trimmed stdout on exit 0, running through the shell in cwd', async () => {
const exec = vi.fn().mockResolvedValue({ code: 0, stdout: ' out\n', stderr: '' });
expect(await runShellCommand('echo out', { cwd: '/x', exec })).toBe('out');
- expect(exec).toHaveBeenCalledWith('echo out', expect.objectContaining({ cwd: '/x' }));
+ // bufferedSpawn signature: (cmd, args, { cwd, timeoutMs, shell })
+ expect(exec).toHaveBeenCalledWith('echo out', [], expect.objectContaining({ cwd: '/x', shell: true }));
});
it('returns null on non-zero exit', async () => {
const exec = vi.fn().mockResolvedValue({ code: 1, stdout: 'partial', stderr: 'err' });
expect(await runShellCommand('false', { cwd: '/x', exec })).toBeNull();
});
+ it('returns null on a timed-out command (code -1)', async () => {
+ const exec = vi.fn().mockResolvedValue({ code: -1, stdout: '', stderr: '', timedOut: true });
+ expect(await runShellCommand('sleep 999', { cwd: '/x', exec })).toBeNull();
+ });
it('returns null on empty command without invoking exec', async () => {
const exec = vi.fn();
expect(await runShellCommand(' ', { cwd: '/x', exec })).toBeNull();