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

fix: fail a tailcat retry that cannot repoint its peer, instead of reporting success

Adam Eivy3cd0d4d84a40
diff --git a/server/services/tailcatPeer.js b/server/services/tailcatPeer.js
index ac8d71aec8..7685c94a3f 100644
--- a/server/services/tailcatPeer.js
+++ b/server/services/tailcatPeer.js
@@ -682,7 +682,9 @@ async function startAndRegister({
// Best-effort, and deliberately before the spawn: on a host whose filter
// blocks Go's dialer this is the difference between a working tunnel and a
// CLI that cannot resolve its own relay.
- await primeDerpMap().catch(() => null);
+ // Promise.resolve().then defers the call so a synchronous throw is caught here
+ // too, the same guard ensureTailcatInstalled documents for its installers.
+ await Promise.resolve().then(primeDerpMap).catch(() => null);
const remotePort = DEFAULT_TAILCAT_REMOTE_PORT;
const localPort = await allocatePort({ preferred: entry.localPort || DEFAULT_TAILCAT_LOCAL_PORT });
@@ -702,8 +704,17 @@ async function startAndRegister({
if (shuttingDown) throw new Error('PortOS is shutting down');
if (peer) {
// A reallocated port has to reach the peer record too, or every request
- // would keep dialing the port the dead forward used to hold.
- if (peer.port !== localPort) peer = await setPeerPortFn(peer.id, localPort) || peer;
+ // would keep dialing the port the dead forward used to hold. A null result
+ // means the record is gone or is no longer a tailcat peer — failing here is
+ // the point: falling back to the stale object would report success while the
+ // peer kept pointing at the dead port.
+ if (peer.port !== localPort) {
+ peer = await setPeerPortFn(peer.id, localPort);
+ if (!peer) throw new ServerError(
+ `Could not repoint peer ${existingPeer.id} at 127.0.0.1:${localPort}`,
+ { status: 409, code: 'TAILCAT_PEER_REPOINT_FAILED' }
+ );
+ }
} else {
peer = await addPeerFn({
address: '127.0.0.1',
@@ -777,7 +788,11 @@ async function retryForward(id, {
killLiveForward(entry.id);
const peers = await getPeersFn();
- const existingPeer = entry.peerId ? peers.find((p) => p.id === entry.peerId) || null : null;
+ // Match the transport too, exactly as the boot-time restore does: a stale entry
+ // whose peerId now names a classic peer must register a fresh one, never adopt it.
+ const existingPeer = entry.peerId
+ ? peers.find((p) => p.id === entry.peerId && p.transport === 'tailcat') || null
+ : null;
const peer = await startAndRegister({
entry, existingPeer, ensureInstalled, primeDerpMap, allocatePort, startForward,
addPeerFn, patchForwardEntry, removePeerFn, setPeerPortFn,
@@ -811,7 +826,7 @@ async function restoreTailcatForwards({
console.log(`⚠️ tailcat restore skipped — ${err.message}`);
return { restored: 0, error: err.message };
}
- await primeDerpMap().catch(() => null);
+ await Promise.resolve().then(primeDerpMap).catch(() => null);
const peers = await getPeersFn();
let restored = 0;
diff --git a/server/services/tailcatPeer.test.js b/server/services/tailcatPeer.test.js
index 77c1f2f01e..1f45244ce3 100644
--- a/server/services/tailcatPeer.test.js
+++ b/server/services/tailcatPeer.test.js
@@ -563,6 +563,47 @@ describe('saved tailcat forwards', () => {
expect(peer.port).toBe(15557);
});
+ it('fails the retry rather than leaving a peer pointed at the dead port', async () => {
+ readJSONFile.mockResolvedValue({ version: 1, forwards: [{
+ id: 'fwd_1', peerId: 'peer-1', tcAddress: EXAMPLE_TC, localPort: 15555, remotePort: 5555,
+ }] });
+ const child = fakeChild();
+ await expect(retryTailcatForward('fwd_1', {
+ ensureInstalled: async () => ({ bin: 'tailcat' }),
+ primeDerpMap: async () => ({ primed: false }),
+ allocatePort: async () => 15557,
+ startForward: async () => child,
+ addPeerFn: async () => { throw new Error('must not register a duplicate'); },
+ patchForwardEntry: async (id, patch) => ({ id, ...patch }),
+ getPeersFn: async () => [{ id: 'peer-1', transport: 'tailcat', address: '127.0.0.1', port: 15555 }],
+ // The record vanished (or stopped being a tailcat peer) mid-retry.
+ setPeerPortFn: async () => null,
+ })).rejects.toMatchObject({ code: 'TAILCAT_PEER_REPOINT_FAILED', status: 409 });
+ expect(child.killed).toBe(true);
+ });
+
+ it('registers a fresh peer when the saved peerId no longer names a tailcat peer', async () => {
+ readJSONFile.mockResolvedValue({ version: 1, forwards: [{
+ id: 'fwd_1', peerId: 'peer-1', tcAddress: EXAMPLE_TC, localPort: 15555, remotePort: 5555,
+ }] });
+ const addPeerFn = vi.fn(async (data) => ({ id: 'peer-new', ...data }));
+ const setPeerPortFn = vi.fn();
+ const peer = await retryTailcatForward('fwd_1', {
+ ensureInstalled: async () => ({ bin: 'tailcat' }),
+ primeDerpMap: async () => ({ primed: false }),
+ allocatePort: async () => 15555,
+ startForward: async () => fakeChild(),
+ addPeerFn,
+ patchForwardEntry: async (id, patch) => ({ id, ...patch }),
+ // Same id, but a classic peer now — adopting it would repoint an unrelated route.
+ getPeersFn: async () => [{ id: 'peer-1', address: '192.0.2.10', port: 5555 }],
+ setPeerPortFn,
+ });
+ expect(setPeerPortFn).not.toHaveBeenCalled();
+ expect(addPeerFn).toHaveBeenCalledOnce();
+ expect(peer.id).toBe('peer-new');
+ });
+
it('records a redacted reason when a retry fails again', async () => {
readJSONFile.mockResolvedValue({ version: 1, forwards: [{
id: 'fwd_1', peerId: null, tcAddress: EXAMPLE_TC, localPort: 15555, remotePort: 5555,