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 (agy): capture full brew link output and settle the install promise once

Adam Eivyfbe816a24e93
diff --git a/server/services/llamaServerManager.js b/server/services/llamaServerManager.js
index b833cf17b6..f8b6165998 100644
--- a/server/services/llamaServerManager.js
+++ b/server/services/llamaServerManager.js
@@ -264,7 +264,11 @@ function linkLlamaCpp(env) {
child.stdout?.on('data', collect);
child.stderr?.on('data', collect);
child.on('error', (err) => resolve({ linked: false, output: err.message }));
- child.on('exit', (code) => resolve({ linked: code === 0, output: output.trim() }));
+ // 'close' rather than 'exit': 'exit' fires the moment the process ends, with
+ // stdio possibly still buffered, which would hand back a truncated (often
+ // empty) `output` exactly when a failure needs explaining. `brew link` is a
+ // short, single-process command, so waiting for the pipes to close is safe.
+ child.on('close', (code) => resolve({ linked: code === 0, output: output.trim() }));
});
}
@@ -297,7 +301,15 @@ export async function installLlamaServer({ onProgress = () => {} } = {}) {
child.stderr?.on('data', (d) => {
onProgress({ event: 'progress', message: d.toString().trim() });
});
+ // `error` and `exit` can both fire for one child (an `error` raised after a
+ // successful spawn is followed by the process's own exit). Without this
+ // guard the exit path would still emit a `complete` progress event to the
+ // UI after the request had already been rejected — the client would render
+ // "installed successfully" alongside a 500.
+ let settled = false;
child.on('error', (err) => {
+ if (settled) return;
+ settled = true;
reject(new ServerError(`Failed to run brew: ${err.message}`, { status: 500 }));
});
// Async listener on a child-process event: it runs outside the Express
@@ -305,6 +317,8 @@ export async function installLlamaServer({ onProgress = () => {} } = {}) {
// unguarded throw here would surface as an unhandled rejection while the
// install request hung forever. Route every failure to `reject`.
child.on('exit', async (code) => {
+ if (settled) return;
+ settled = true;
try {
let binaryPath = resolveLlamaServerBinary();
let linkOutput = '';
diff --git a/server/services/llamaServerManager.test.js b/server/services/llamaServerManager.test.js
index 58793fe968..eaa383c150 100644
--- a/server/services/llamaServerManager.test.js
+++ b/server/services/llamaServerManager.test.js
@@ -18,6 +18,14 @@ function fakeSpawnProcess() {
return child;
}
+// A real child emits 'exit' and then 'close' (once stdio has flushed). Mirror
+// both so the mocks stay honest for either listener: `brew install` is awaited
+// on 'exit', while the `brew link` step waits for 'close' to capture output.
+function endProcess(child, code) {
+ child.emit('exit', code);
+ child.emit('close', code);
+}
+
describe('llamaServerManager', () => {
beforeEach(() => {
_resetLlamaServerStateForTests();
@@ -134,7 +142,7 @@ describe('llamaServerManager', () => {
fakeChild.stderr = new EventEmitter();
vi.spyOn(childProcess, 'spawn').mockImplementation(() => {
- setTimeout(() => fakeChild.emit('exit', 0), 10);
+ setTimeout(() => endProcess(fakeChild, 0), 10);
return fakeChild;
});
@@ -159,7 +167,7 @@ describe('llamaServerManager', () => {
vi.spyOn(childProcess, 'spawn').mockImplementation((cmd, args) => {
spawnCalls.push({ cmd, args });
const child = args[0] === 'install' ? installChild : linkChild;
- setTimeout(() => child.emit('exit', 0), 10);
+ setTimeout(() => endProcess(child, 0), 10);
return child;
});
@@ -187,9 +195,9 @@ describe('llamaServerManager', () => {
setTimeout(() => {
if (args[0] === 'link') {
child.stderr.emit('data', Buffer.from('Error: Could not symlink bin/llama-server'));
- child.emit('exit', 1);
+ endProcess(child, 1);
} else {
- child.emit('exit', 0);
+ endProcess(child, 0);
}
}, 10);
return child;
@@ -208,7 +216,7 @@ describe('llamaServerManager', () => {
vi.spyOn(childProcess, 'spawn').mockImplementation((cmd, args) => {
if (args[0] === 'link') throw new Error('spawn EACCES');
const child = fakeSpawnProcess();
- setTimeout(() => child.emit('exit', 0), 10);
+ setTimeout(() => endProcess(child, 0), 10);
return child;
});
@@ -221,7 +229,7 @@ describe('llamaServerManager', () => {
vi.spyOn(childProcess, 'spawn').mockImplementation(() => {
const child = fakeSpawnProcess();
- setTimeout(() => child.emit('exit', 0), 10);
+ setTimeout(() => endProcess(child, 0), 10);
return child;
});