Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(view): a busy port names a fix the hosted page can find [N26,I1]

Corey Olson9ff3f6825c98
diff --git a/src/cli.js b/src/cli.js
index 8e575399..d9b9ded2 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -74,7 +74,7 @@ import { milestoneRoadmap } from './roadmap.js';
import { listResources, memoryVerdict, readMachineMemory, runnerResourceStatus } from './resources.js';
import { citedTestFiles, rowEvidence, rowStage } from './evidence.js';
import { exportView, serveView } from './serve.js';
-import { forgetView, rememberView, runningView } from './view-session.js';
+import { forgetView, portHolder, rememberView, runningView } from './view-session.js';
import { serveApi } from './api.js';
import { serveMcp } from './mcp.js';
import { doctorProblems, doctrineProblems } from './doctor.js';
@@ -1510,7 +1510,14 @@ async function viewHere(io, values) {
} catch (error) {
if (view) await view.close();
if (error instanceof Refused && error.code === 'PORT_BUSY') {
- error = new Refused('PORT_BUSY', `port ${port} is in use: name another with --port N, or use --port 0 to take a free one`);
+ // pullboard.dev only looks on the default port, so a busy default names the holder to stop.
+ // The background child is handed --port explicitly, so the default is matched by number.
+ let hosted = false;
+ try { hosted = port !== 0 && port === defaultViewPort(); } catch { /* A malformed override names no default port. */ }
+ const holder = hosted ? portHolder(port) : null;
+ const stop = holder && (process.platform === 'win32' ? `taskkill /PID ${holder.pid} /T` : `kill ${holder.pid}`);
+ error = new Refused('PORT_BUSY', !hosted ? `port ${port} is in use: name another with --port N, or use --port 0 to take a free one`
+ : `port ${port} is in use${holder ? ` by ${holder.name} (pid ${holder.pid})` : ''}, and pullboard.dev looks for your view on that port: stop ${holder ? `it with ${stop}` : 'the other view there'}, then run pullboard view again`);
}
if (process.env.PULLBOARD_VIEW_BACKGROUND_CHILD === '1' && process.send) {
process.send({ type: 'startup-error', code: error.code ?? 'VIEW_START', message: error.message });
diff --git a/src/next-steps.js b/src/next-steps.js
index 27dad074..707e710d 100644
--- a/src/next-steps.js
+++ b/src/next-steps.js
@@ -261,7 +261,7 @@ export const NEXT_STEPS = Object.freeze({
PHONE_NOT_PAIRED: 'pair the phone: pullboard relay on --all',
PIN_FAILED: 'fix what git reported, then run pullboard run again',
PIPEFAIL_UNAVAILABLE: 'rewrite the gate in pullboard.json without a pipe',
- PORT_BUSY: 'pick a free port with --port 0',
+ PORT_BUSY: 'stop the other view on that port, then run pullboard view again',
PROJECT_NOT_REGISTERED: 'run pullboard view --project inside a registered repo, or plain pullboard view',
PROJECT_SCOPE: 'scope the API to one registered project repo',
PROOF_REQUIRED: 'say how you proved it: --note "what you broke or which edge you tried, and what happened"',
diff --git a/src/view-session.js b/src/view-session.js
index 547b31e5..99010acb 100644
--- a/src/view-session.js
+++ b/src/view-session.js
@@ -1,5 +1,6 @@
/** Private runtime records that let an unattended CLI hand off its live view link. */
-import { chmodSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
+import { spawnSync } from 'node:child_process';
+import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { LOOPBACK, lastPort } from './serve.js';
import { registryFile } from './projects.js';
@@ -78,3 +79,27 @@ export async function runningView(port = 0, expectedScopeCommonDir = null) {
return null;
}
}
+
+/**
+ * Name the process listening on a loopback port, when this machine can tell: first a live view
+ * recorded for that port, then lsof where the system has it. Returns null when neither knows.
+ * Only the pid and the executable's own name are kept, since arguments can carry secrets.
+ *
+ * @param {number} port
+ * @returns {{ pid: number, name: string } | null}
+ */
+export function portHolder(port) {
+ const recorded = readSession(port);
+ if (recorded) {
+ try {
+ process.kill(recorded.pid, 0);
+ return { pid: recorded.pid, name: 'pullboard view' };
+ } catch { /* The recorded view has exited; ask the system who holds the port instead. */ }
+ }
+ const lsof = ['/usr/sbin/lsof', '/usr/bin/lsof'].find((file) => existsSync(file));
+ if (!lsof) return null;
+ const listed = spawnSync(lsof, ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-Fpc'], { encoding: 'utf8', timeout: 2_000 });
+ const pid = Number(/^p(\d+)$/mu.exec(listed.stdout ?? '')?.[1]);
+ if (!Number.isInteger(pid) || pid < 1) return null;
+ return { pid, name: /^c(.+)$/mu.exec(listed.stdout)?.[1]?.trim() || 'a program' };
+}
diff --git a/test/view-port.test.js b/test/view-port.test.js
index c2126113..23a8b75f 100644
--- a/test/view-port.test.js
+++ b/test/view-port.test.js
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
import { runFixtureChild, startFixtureChild } from './fixture-child.js';
import { createServer } from 'node:net';
import { createServer as createHttpServer } from 'node:http';
-import { mkdtempSync, readFileSync, readdirSync, rmSync, statSync } from 'node:fs';
+import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
@@ -107,6 +107,51 @@ test("a named port that is busy is refused with the way out, not a stack trace [
assert.equal(result.stderr.trim(), `pullboard: [PORT_BUSY] port ${port} is in use: name another with --port N, or use --port 0 to take a free one\nnext: ${refusal.next}`);
});
+test('a busy view port names a fix the hosted page can still find [N26,I1]', async (t) => {
+ const dir = mkdtempSync(join(tmpdir(), 'pullboard-view-busy-default-'));
+ t.after(() => rmSync(dir, { recursive: true, force: true }));
+ // A leased stand-in for 4890: the person's real view on the shipped port is never touched.
+ const lease = await takeDefaultViewPort(dir, { agent: `view-port-busy-${process.pid}` });
+ t.after(() => lease.release());
+ const port = lease.port;
+ const held = createServer();
+ await new Promise((done, fail) => { held.once('error', fail); held.listen(port, '127.0.0.1', done); });
+ t.after(() => new Promise((done) => held.close(done)));
+ const env = {
+ ...process.env,
+ HOME: join(dir, 'home'),
+ PULLBOARD_HOME: join(dir, 'home', '.pullboard'),
+ PULLBOARD_MACHINE_HOME: join(dir, 'machine-home'),
+ PULLBOARD_VIEW_PORT: String(port),
+ GIT_CONFIG_GLOBAL: '/dev/null',
+ GIT_CONFIG_NOSYSTEM: '1',
+ };
+ delete env.AI_AGENT;
+ delete env.PULLBOARD_VIEW_FOREGROUND;
+ const plain = runFixtureChild(process.execPath, [BIN, 'view', '--no-open'], { cwd: dir, env, encoding: 'utf8', timeout: 40_000 });
+ assert.equal(plain.status, 1, plain.failure ?? plain.stderr);
+ const json = runFixtureChild(process.execPath, [BIN, 'view', '--no-open', '--json'], { cwd: dir, env, encoding: 'utf8', timeout: 40_000 });
+ assert.equal(json.status, 1, json.failure ?? json.stderr);
+ const refusal = JSON.parse(json.stdout).error;
+ assert.equal(refusal.code, 'PORT_BUSY');
+ for (const text of [refusal.message, refusal.next, plain.stderr]) {
+ assert.ok(!text.includes('--port 0'), `no step the hosted page can never find: ${text}`);
+ }
+ assert.match(refusal.message, new RegExp(`^port ${port} is in use`, 'u'));
+ assert.match(refusal.message, /pullboard\.dev looks for your view on that port: stop .+, then run pullboard view again$/u,
+ 'the refusal says the one fix that keeps the hosted page working');
+ if (['/usr/sbin/lsof', '/usr/bin/lsof'].some((file) => existsSync(file)) && process.platform !== 'win32') {
+ assert.ok(refusal.message.includes(`in use by node (pid ${process.pid}),`) && refusal.message.includes(`stop it with kill ${process.pid},`),
+ `the refusal names the process holding the port: ${refusal.message}`);
+ }
+ assert.equal(refusal.next, 'stop the other view on that port, then run pullboard view again');
+ assert.equal(plain.stderr.trim(), `pullboard: [PORT_BUSY] ${refusal.message}\nnext: ${refusal.next}`);
+ const free = runFixtureChild(process.execPath, [BIN, 'view', '--no-open', '--port', '0', '--json'], { cwd: dir, env, encoding: 'utf8', timeout: 40_000 });
+ assert.equal(free.status, 0, free.failure ?? free.stderr);
+ t.after(() => cleanupFixtureViews(env.PULLBOARD_HOME));
+ assert.notEqual(JSON.parse(free.stdout).port, port, '--port 0 still takes a free port when asked for');
+});
+
/** Wait until the owned default listener has closed after its process stops. */
async function waitForPortToClose(port, request = fetchFresh) {
const deadline = Date.now() + 5_000; // budget: hang ceiling for observing the owned view's graceful close.
@@ -296,7 +341,7 @@ test('pullboard view uses its fixed default port and reuses its authenticated li
const refusal = JSON.parse(busy.stdout).error;
assert.equal(refusal.code, 'PORT_BUSY');
assert.ok(refusal.message.includes(String(port)));
- assert.match(refusal.message, /--port/u);
+ assert.match(refusal.message, /: stop .+, then run pullboard view again$/u);
assert.equal(await (await fetchFresh(`http://127.0.0.1:${port}/`)).text(), 'unrelated listener', 'the refusal leaves another listener untouched');
} finally {
if (unrelated.listening) await new Promise((done) => unrelated.close(done));