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

refactor: take the install-refusal reason directly and harden the reason-code source guard (#3579)

[._.]/ Adam Eivyd2890e12d2a4
diff --git a/server/routes/imageTo3d.genericDispatch.test.js b/server/routes/imageTo3d.genericDispatch.test.js
index 73e4146411..fa87d94a64 100644
--- a/server/routes/imageTo3d.genericDispatch.test.js
+++ b/server/routes/imageTo3d.genericDispatch.test.js
@@ -24,7 +24,6 @@ vi.mock('../services/imageTo3d/targets.js', async (importOriginal) => ({
detectHostCapabilities: vi.fn(() => ({ appleSilicon: true, unifiedMemoryGb: 128, cuda: false })),
getTarget: vi.fn((id) => (id === 'fakegen' ? FAKE_TARGET : null)),
listTargets: vi.fn(() => [FAKE_TARGET]),
- isTargetAvailable: vi.fn(() => true),
unavailableReason: vi.fn(() => null),
IMAGE_TO_3D_TARGET_IDS: ['fakegen'],
}));
diff --git a/server/routes/imageTo3d.js b/server/routes/imageTo3d.js
index 3338969968..c99fe493af 100644
--- a/server/routes/imageTo3d.js
+++ b/server/routes/imageTo3d.js
@@ -7,7 +7,6 @@ import {
getTarget,
listTargets,
detectHostCapabilities,
- isTargetAvailable,
unavailableReason,
unavailableReasonLabel,
IMAGE_TO_3D_TARGET_IDS,
@@ -104,12 +103,15 @@ async function handleTargetInstall(targetId, req, res) {
// Refuse on unsupported hardware rather than clone a multi-GB install that can
// never run.
const capabilities = await detectHostCapabilities();
- if (!isTargetAvailable(targetId, capabilities)) {
+ // `unavailableReason` is what `isTargetAvailable` asks anyway — take the reason
+ // directly so the refusal doesn't re-run the gate to name what blocked it.
+ const blockedReason = unavailableReason(targetId, capabilities);
+ if (blockedReason !== null) {
send({
type: 'error',
// The label, not the raw kebab-case code — this string is read by a human
// in the install log, and `requires-linux-host` doesn't tell them to use WSL2.
- message: `This host cannot run ${target.label} (${unavailableReasonLabel(unavailableReason(targetId, capabilities))}). Install skipped.`,
+ message: `This host cannot run ${target.label} (${unavailableReasonLabel(blockedReason)}). Install skipped.`,
});
return safeEnd();
}
diff --git a/server/routes/imageTo3d.test.js b/server/routes/imageTo3d.test.js
index 0f4688f9e4..7e8ab1ddfd 100644
--- a/server/routes/imageTo3d.test.js
+++ b/server/routes/imageTo3d.test.js
@@ -34,8 +34,7 @@ vi.mock('../services/imageTo3d/targets.js', async (importOriginal) => ({
listTargets: vi.fn((caps) => [
{ ...TRELLIS2_TARGET, available: caps.appleSilicon && caps.unifiedMemoryGb >= 24 },
]),
- isTargetAvailable: vi.fn(() => true),
- unavailableReason: vi.fn(() => 'requires-apple-silicon'),
+ unavailableReason: vi.fn(() => null),
IMAGE_TO_3D_TARGET_IDS: ['trellis2'],
}));
@@ -175,7 +174,6 @@ describe('image-to-3d routes', () => {
describe('GET /trellis2/install (SSE)', () => {
it('streams stage → complete on the happy path', async () => {
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
const res = await request(makeApp()).get('/api/image-to-3d/trellis2/install');
const frames = sseFrames(res.text);
expect(frames).toContainEqual({ type: 'stage', stage: 'clone', message: 'git clone …' });
@@ -188,7 +186,6 @@ describe('GET /trellis2/install (SSE)', () => {
it('tells the install to download the Metal Toolchain when it is missing but fetchable', async () => {
trellis2.installTrellis2.mockClear();
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
trellis2.probeMetalToolchain.mockResolvedValueOnce({
available: false, installable: true, hint: 'it will be downloaded',
});
@@ -204,7 +201,6 @@ describe('GET /trellis2/install (SSE)', () => {
it('warns but still installs when the toolchain cannot be fetched (Command Line Tools only)', async () => {
trellis2.installTrellis2.mockClear();
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
trellis2.probeMetalToolchain.mockResolvedValueOnce({
available: false, installable: false, blocker: 'requires-xcode', hint: 'install Xcode',
});
@@ -219,7 +215,6 @@ describe('GET /trellis2/install (SSE)', () => {
it('adds no toolchain step or warning when it is already present', async () => {
trellis2.installTrellis2.mockClear();
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
const res = await request(makeApp()).get('/api/image-to-3d/trellis2/install');
expect(sseFrames(res.text).find((f) => f.stage === 'preflight')).toBeUndefined();
expect(trellis2.installTrellis2).toHaveBeenCalledWith(
@@ -232,7 +227,6 @@ describe('GET /trellis2/install (SSE)', () => {
// existing install once the Metal Toolchain is present (#2952).
trellis2.installTrellis2.mockClear();
trellis2.isTrellis2Installed.mockReturnValueOnce(true);
- targets.isTargetAvailable.mockReturnValueOnce(true);
const res = await request(makeApp()).get('/api/image-to-3d/trellis2/install?repair=1');
expect(trellis2.installTrellis2).toHaveBeenCalled();
expect(sseFrames(res.text).at(-1)).toMatchObject({ type: 'complete' });
@@ -254,7 +248,6 @@ describe('GET /trellis2/install (SSE)', () => {
// not just whatever the server process happened to be launched with.
trellis2.installTrellis2.mockClear();
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
await request(makeApp()).get('/api/image-to-3d/trellis2/install');
expect(trellis2.installTrellis2).toHaveBeenCalledWith(expect.objectContaining({
env: expect.objectContaining({ HF_TOKEN: 'hf_test', HUGGINGFACE_HUB_TOKEN: 'hf_test' }),
@@ -266,7 +259,6 @@ describe('GET /trellis2/install (SSE)', () => {
// reach the error middleware as JSON; it has to surface as an error frame.
trellis2.installTrellis2.mockClear();
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
hfChildEnv.mockRejectedValueOnce(new Error('settings.json is not valid JSON'));
const res = await request(makeApp()).get('/api/image-to-3d/trellis2/install');
const frames = sseFrames(res.text);
@@ -289,7 +281,6 @@ describe('GET /trellis2/install (SSE)', () => {
it('appends a resume hint when the install fails with a transient network error', async () => {
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
trellis2.installTrellis2.mockImplementationOnce(() => ({
promise: Promise.reject(Object.assign(
new Error("TRELLIS.2 install step 'setup' exited 128"),
@@ -308,7 +299,6 @@ describe('GET /trellis2/install (SSE)', () => {
it('does NOT append the resume hint for a non-transient failure', async () => {
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(true);
trellis2.installTrellis2.mockImplementationOnce(() => ({
promise: Promise.reject(Object.assign(
new Error('TRELLIS.2 install step \'setup\' exited 1'),
@@ -325,7 +315,6 @@ describe('GET /trellis2/install (SSE)', () => {
it('refuses on unsupported hardware', async () => {
trellis2.installTrellis2.mockClear();
trellis2.isTrellis2Installed.mockReturnValueOnce(false);
- targets.isTargetAvailable.mockReturnValueOnce(false);
targets.unavailableReason.mockReturnValueOnce('requires-apple-silicon');
const res = await request(makeApp()).get('/api/image-to-3d/trellis2/install');
const frames = sseFrames(res.text);
diff --git a/server/services/imageTo3d/unavailableReasons.parity.test.js b/server/services/imageTo3d/unavailableReasons.parity.test.js
index 48649e3d2b..3c31262236 100644
--- a/server/services/imageTo3d/unavailableReasons.parity.test.js
+++ b/server/services/imageTo3d/unavailableReasons.parity.test.js
@@ -33,17 +33,24 @@ import {
unavailableReasonLabel as clientLabel,
} from '../../../client/src/lib/imageTo3dReasons.js';
-// Every kebab-case string literal inside `unavailableReason()` is a reason code:
-// the only other literal in the body is `'unknown'` (the cudaProbe status), which
-// has no dash and so falls outside this pattern.
+// Reason codes are kebab-case (asserted below), and every OTHER single-quoted
+// literal in `unavailableReason()` is a dash-free status token (`'unknown'`, the
+// cudaProbe state) — so "quoted, has a dash" identifies the codes exactly.
+// Comments are stripped first so a code named in prose can't count as a return.
+const KEBAB_CODE = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)+$/;
+
function reasonCodesInSource() {
const src = readFileSync(fileURLToPath(new URL('./targets.js', import.meta.url)), 'utf8');
const start = src.indexOf('export function unavailableReason(');
- const end = src.indexOf('\nexport function isTargetAvailable');
expect(start, 'unavailableReason() not found in targets.js').toBeGreaterThan(-1);
- expect(end, 'isTargetAvailable() not found in targets.js').toBeGreaterThan(start);
- const body = src.slice(start, end);
- return [...body.matchAll(/'([a-z][a-z0-9]*(?:-[a-z0-9]+)+)'/g)].map((m) => m[1]);
+ // End at whatever export follows — robust to the next declaration being
+ // renamed, reordered, or having a new export inserted before it.
+ const after = src.indexOf('\nexport ', start + 1);
+ expect(after, 'no export follows unavailableReason() — cannot bound its body').toBeGreaterThan(start);
+ const body = src.slice(start, after)
+ .replace(/\/\*[\s\S]*?\*\//g, '')
+ .replace(/\/\/[^\n]*/g, '');
+ return [...body.matchAll(/'([a-z][a-z0-9-]*)'/g)].map((m) => m[1]).filter((s) => KEBAB_CODE.test(s));
}
describe('image-to-3D unavailable reasons — server↔client parity', () => {
@@ -64,6 +71,14 @@ describe('image-to-3D unavailable reasons — server↔client parity', () => {
});
describe('image-to-3D unavailable reasons — code coverage', () => {
+ // The source guard below identifies codes by their kebab shape, so the shape
+ // is part of the contract: a dash-free code would slip past it unlabelled.
+ it('keeps every code kebab-case', () => {
+ for (const code of Object.keys(SERVER_REASONS)) {
+ expect(code, `${code} must be kebab-case for the source guard to see it`).toMatch(KEBAB_CODE);
+ }
+ });
+
it('labels exactly the codes unavailableReason() can return', () => {
const codes = reasonCodesInSource();
expect(codes.length).toBeGreaterThan(0);