Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Answer review: skip a failed page build, and try the bundle's build in PR CI

Simon Flack466ac88677e4
diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml
index b89465390..9a4ae6ef1 100644
--- a/.github/workflows/pr-ci.yml
+++ b/.github/workflows/pr-ci.yml
@@ -75,6 +75,7 @@ jobs:
migrations: ${{ steps.filter.outputs.migrations }}
chart: ${{ steps.filter.outputs.chart }}
e2e: ${{ steps.filter.outputs.e2e }}
+ local_server_bundle: ${{ steps.filter.outputs.local_server_bundle }}
steps:
- name: Checkout
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
@@ -114,6 +115,15 @@ jobs:
gateway:
- *shared
- 'gateway/**'
+ # The step of the local-server bundle that builds the gateway's
+ # pages. The bundle itself is only built at release, so the console
+ # jobs below run this step alone when it, or what it installs, changes.
+ local_server_bundle:
+ - *shared
+ - 'console/apps/switch-console-desktop/scripts/build-gateway-ui.mjs'
+ - 'console/apps/switch-console-desktop/scripts/build-local-server-bundle.mjs'
+ - 'gateway/package.json'
+ - 'gateway/package-lock.json'
# The server, the stack the fast lane stands up, and the harness.
e2e:
- *shared
@@ -296,6 +306,11 @@ jobs:
- name: Test (vitest)
run: pnpm -r run test
+ # The local-server bundle's gateway step, which otherwise first runs at release.
+ - name: Build the local server's gateway pages
+ if: ${{ !cancelled() && (needs.changes.outputs.local_server_bundle == 'true' || github.event_name == 'workflow_dispatch') }}
+ run: node apps/switch-console-desktop/scripts/build-gateway-ui.mjs ../gateway "$RUNNER_TEMP/gateway-ui"
+
# The agent runner and the controller on native Windows (CHOO-3472): process
# control, CLI lookup and permissions there take different code paths, and
# their tests that start real processes only run on Windows. Scoped to the
@@ -347,6 +362,11 @@ jobs:
if: ${{ !cancelled() }}
run: pnpm --filter @switch-console/desktop exec vitest run --project node src/main/core/embedded-controller src/main/core/execution-context
+ # The local-server bundle's gateway step, which runs npm through a shell on Windows.
+ - name: Build the local server's gateway pages
+ if: ${{ !cancelled() && (needs.changes.outputs.local_server_bundle == 'true' || github.event_name == 'workflow_dispatch') }}
+ run: node apps/switch-console-desktop/scripts/build-gateway-ui.mjs ../gateway "$RUNNER_TEMP/gateway-ui"
+
artifacts:
name: Artifact registry
# Every artifact carries a compiled-in copy of artifacts.yaml, so the copies
diff --git a/console/apps/switch-console-desktop/scripts/build-gateway-ui.mjs b/console/apps/switch-console-desktop/scripts/build-gateway-ui.mjs
new file mode 100644
index 000000000..5ea376a5a
--- /dev/null
+++ b/console/apps/switch-console-desktop/scripts/build-gateway-ui.mjs
@@ -0,0 +1,45 @@
+/**
+ * Build the gateway's pages (the admin dashboard) from a Switch source tree's
+ * `gateway/` and copy the build to a directory, as the published image builds
+ * them. The local-server bundle uses it for the release it bundles; PR CI runs
+ * it on its own, against the branch's `gateway/`, so a change to this step is
+ * tried on Linux and Windows before a release depends on it:
+ *
+ * node scripts/build-gateway-ui.mjs <gateway source dir> <destination>
+ */
+import { execFileSync } from 'node:child_process';
+import { existsSync } from 'node:fs';
+import { cp, rm } from 'node:fs/promises';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+export async function buildGatewayUi(gatewaySource, destination) {
+ // The build tools are dev dependencies, which npm drops under
+ // NODE_ENV=production; Windows' npm is a .cmd script, run through a shell.
+ const env = { ...process.env };
+ delete env.NODE_ENV;
+ const npm = (args) =>
+ execFileSync('npm', args, {
+ cwd: gatewaySource,
+ stdio: 'inherit',
+ env,
+ shell: process.platform === 'win32',
+ });
+ npm(['ci', '--no-audit', '--no-fund']);
+ npm(['run', 'build']);
+ await rm(destination, { recursive: true, force: true });
+ await cp(path.join(gatewaySource, 'dist'), destination, { recursive: true });
+ if (!existsSync(path.join(destination, 'index.html'))) {
+ throw new Error(`The gateway build in ${gatewaySource} produced no index.html`);
+ }
+}
+
+if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
+ const [source, destination] = process.argv.slice(2);
+ if (!source || !destination) {
+ console.error('usage: node scripts/build-gateway-ui.mjs <gateway source dir> <destination>');
+ process.exit(2);
+ }
+ await buildGatewayUi(path.resolve(source), path.resolve(destination));
+ console.log(`gateway pages built into ${path.resolve(destination)}`);
+}
diff --git a/console/apps/switch-console-desktop/scripts/build-local-server-bundle.mjs b/console/apps/switch-console-desktop/scripts/build-local-server-bundle.mjs
index 4c72eedea..d418b5828 100644
--- a/console/apps/switch-console-desktop/scripts/build-local-server-bundle.mjs
+++ b/console/apps/switch-console-desktop/scripts/build-local-server-bundle.mjs
@@ -42,6 +42,7 @@ import path from 'node:path';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { build } from 'esbuild';
+import { buildGatewayUi } from './build-gateway-ui.mjs';
const OUT = path.resolve('dist-local-server');
@@ -270,25 +271,7 @@ await cp(
// The release's gateway pages, built from the same source, as the published
// image builds them: switch-core serves them when GATEWAY_UI_DIR names them.
console.log(`local-server bundle: gateway pages for switch-core ${switchVersion}`);
-const gatewaySource = path.join(source, 'gateway');
-// The build tools are dev dependencies, which npm drops under
-// NODE_ENV=production; Windows' npm is a .cmd script, run through a shell.
-const npmEnv = { ...process.env };
-delete npmEnv.NODE_ENV;
-const npm = (args) =>
- execFileSync('npm', args, {
- cwd: gatewaySource,
- stdio: 'inherit',
- env: npmEnv,
- shell: process.platform === 'win32',
- });
-npm(['ci', '--no-audit', '--no-fund']);
-npm(['run', 'build']);
-const gatewayUi = path.join(switchDir, 'gateway-ui');
-await cp(path.join(gatewaySource, 'dist'), gatewayUi, { recursive: true });
-if (!existsSync(path.join(gatewayUi, 'index.html'))) {
- throw new Error(`The gateway build in ${gatewaySource} produced no index.html`);
-}
+await buildGatewayUi(path.join(source, 'gateway'), path.join(switchDir, 'gateway-ui'));
const requirements = execFileSync(
uv,
['export', '--frozen', '--no-dev', '--no-emit-project', '--format', 'requirements-txt', '-q'],
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.test.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.test.ts
index 39a917444..fc30be63a 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.test.ts
@@ -9,7 +9,10 @@ import { buildNativeCoreSettings } from './core-settings';
import {
buildGatewayUi,
checkoutGatewayUi,
+ downloadedGatewayUiWouldBuild,
GATEWAY_UI_ENV,
+ gatewayUiBuildFailed,
+ gatewayUiFailedMarker,
hasGatewayUiBuild,
nativeAdminPages,
runningServesAdminPages,
@@ -234,6 +237,21 @@ describe('building the pages from source', () => {
'utf8'
);
expect(buildLog).toContain('vite: build failed');
+ // Recorded, so later starts of this version skip the same failing build.
+ expect(await gatewayUiBuildFailed(path.join(root, 'gateway-ui'))).toBe(true);
+ expect(messages.at(-1)).toContain(gatewayUiFailedMarker(path.join(root, 'gateway-ui')));
+ });
+
+ it('stops offering a restart for a version whose build failed', async () => {
+ const layout = nativeLayout(path.join(root, 'server'));
+ // A dev build with no bundle or checkout builds the pages on its next start…
+ expect(await downloadedGatewayUiWouldBuild(layout, '0.31.0')).toBe(true);
+ // …unless that build already failed for this version: a restart would not retry it.
+ const pages = path.join(layout.switchCore, '0.31.0', 'gateway-ui');
+ await mkdir(path.dirname(pages), { recursive: true });
+ await writeFile(gatewayUiFailedMarker(pages), 'failed');
+ expect(await downloadedGatewayUiWouldBuild(layout, '0.31.0')).toBe(false);
+ expect(await downloadedGatewayUiWouldBuild(layout, '0.32.0')).toBe(true);
});
it('has nothing to build from a source without the gateway', async () => {
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.ts
index 2c1a0e043..0a076d159 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/gateway-ui.ts
@@ -1,4 +1,4 @@
-import { appendFile, access, mkdir, readFile, rename, rm } from 'node:fs/promises';
+import { appendFile, access, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { log } from '@main/lib/logger';
import type { LocalServerAdminPages } from '@shared/core/managed-switch-server/managed-switch-server';
@@ -26,6 +26,35 @@ export const GATEWAY_UI_ENV = 'GATEWAY_UI_DIR';
const BUILD_TIMEOUT_MS = 15 * 60 * 1000;
+/**
+ * Left beside an install's pages when building them failed, so later starts
+ * of that version do not run the same failing build again (and Settings does
+ * not offer a restart that would). A new install of the version clears it;
+ * deleting it by hand asks for another try.
+ */
+export function gatewayUiFailedMarker(destination: string): string {
+ return `${destination}.failed`;
+}
+
+/** Whether a dev build's next start of downloaded switch-core `version`
+ * would build the pages: not when building them for it already failed. */
+export async function downloadedGatewayUiWouldBuild(
+ layout: NativeLayout,
+ version: string
+): Promise<boolean> {
+ return !(await gatewayUiBuildFailed(path.join(layout.switchCore, version, GATEWAY_UI_DIR_NAME)));
+}
+
+/** Whether building the pages into `destination` failed before. */
+export async function gatewayUiBuildFailed(destination: string): Promise<boolean> {
+ try {
+ await access(gatewayUiFailedMarker(destination));
+ return true;
+ } catch {
+ return false;
+ }
+}
+
/** Whether `dir` holds a build of the pages switch-core will accept. */
export async function hasGatewayUiBuild(dir: string): Promise<boolean> {
try {
@@ -101,7 +130,12 @@ export async function buildGatewayUi(opts: {
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
log.warn('local-switch-server: could not build the admin pages', { error: reason });
- onMessage(`Could not build the admin pages, so the server starts without them (${buildLog})`);
+ const failed = gatewayUiFailedMarker(destination);
+ await writeFile(failed, `${new Date().toISOString()} ${reason}\n`).catch(() => undefined);
+ onMessage(
+ `Could not build the admin pages, so the server starts without them (${buildLog}). ` +
+ `Delete ${failed} and restart to try again.`
+ );
return null;
} finally {
await appendFile(buildLog, `${lines.join('\n')}\n`).catch(() => undefined);
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.ts
index 2fef64e03..b7643031a 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/native-stack.ts
@@ -51,7 +51,7 @@ import {
waitForCoreHealth,
} from './core-process';
import { buildNativeCoreSettings, nativeApiUrl } from './core-settings';
-import { checkoutGatewayUi, nativeAdminPages } from './gateway-ui';
+import { checkoutGatewayUi, downloadedGatewayUiWouldBuild, nativeAdminPages } from './gateway-ui';
import {
mattermostCallbackBaseUrl,
pullMattermostImage,
@@ -614,16 +614,19 @@ export function nativeServerTelemetryEndpoint(): string {
/**
* Whether a start now would hand switch-core the gateway's pages: the
* checkout has a build of them, this build's bundle ships them, or (dev
- * builds) a downloaded install would build them.
+ * builds) a downloaded install would build them — unless building them for
+ * that version already failed, which a restart would not retry.
*/
export async function nativeAdminPagesOnRestart(
+ layout: NativeLayout,
checkoutRoot: string | null,
bundleRoot: string | null
): Promise<boolean> {
if (checkoutRoot !== null) return (await checkoutGatewayUi(checkoutRoot)) !== null;
- const bundle = await readLocalServerBundle(bundleRoot, managedSwitchVersion());
+ const version = managedSwitchVersion();
+ const bundle = await readLocalServerBundle(bundleRoot, version);
if (bundle !== null) return bundle.gatewayUi !== null;
- return import.meta.env.DEV;
+ return import.meta.env.DEV && (await downloadedGatewayUiWouldBuild(layout, version));
}
/** What the running native server's admin pages are doing, for its page to
@@ -633,7 +636,9 @@ export function readNativeAdminPages(
checkoutRoot: string | null,
bundleRoot: string | null
): Promise<LocalServerAdminPages> {
- return nativeAdminPages(layout, () => nativeAdminPagesOnRestart(checkoutRoot, bundleRoot));
+ return nativeAdminPages(layout, () =>
+ nativeAdminPagesOnRestart(layout, checkoutRoot, bundleRoot)
+ );
}
export async function readNativeTelemetry(layout: NativeLayout): Promise<DeployedTelemetry> {
diff --git a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/switch-core-install.ts b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/switch-core-install.ts
index a19b3e479..1d5145e7a 100644
--- a/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/switch-core-install.ts
+++ b/console/apps/switch-console-desktop/src/main/core/managed-switch-server/native/switch-core-install.ts
@@ -10,6 +10,8 @@ import {
buildGatewayUi,
checkoutGatewayUi,
GATEWAY_UI_DIR_NAME,
+ gatewayUiBuildFailed,
+ gatewayUiFailedMarker,
gatewayUiOrNull,
} from './gateway-ui';
import type { NativeLayout } from './native-paths';
@@ -175,14 +177,15 @@ export async function installSwitchCore(opts: {
const venv = path.join(dir, 'venv');
const python = venvPython(venv);
const gatewayUi = path.join(dir, GATEWAY_UI_DIR_NAME);
- // A downloaded install whose pages are missing (a build that failed, or one
- // made before they were built) gets another go at them; a bundled one has
- // whatever its bundle shipped.
+ // A downloaded install whose pages are missing (one made before they were
+ // built) gets a go at them, unless building them for this install already
+ // failed; a bundled one has whatever its bundle shipped.
const pagesFor = async (source: 'bundle' | 'download'): Promise<string | null> => {
const existing = await gatewayUiOrNull(gatewayUi);
if (existing !== null || source === 'bundle' || !opts.buildGatewayUiFromSource) {
return existing;
}
+ if (await gatewayUiBuildFailed(gatewayUi)) return null;
return buildGatewayUi({ layout, sourceRoot: src, destination: gatewayUi, onMessage });
};
// Checked by running it, not by its file existing: an environment made from
@@ -223,6 +226,7 @@ export async function installSwitchCore(opts: {
await uvSync(uv, layout, projectDir, venv);
await writeMarker(dir, { version, source: 'download', uv: uv.version });
await rm(gatewayUi, { recursive: true, force: true });
+ await rm(gatewayUiFailedMarker(gatewayUi), { force: true });
return { version, sourceRoot: src, projectDir, python, gatewayUiDir: await pagesFor('download') };
}