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 Petr's review: restart offers, repeatable failures, CI trigger

Simon Flackccb8852afc3b
diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml
index 9a4ae6ef1..121ec5969 100644
--- a/.github/workflows/pr-ci.yml
+++ b/.github/workflows/pr-ci.yml
@@ -117,7 +117,8 @@ jobs:
- '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.
+ # jobs below run (with this step) when it, or what it installs,
+ # changes, even where nothing else in console/ did.
local_server_bundle:
- *shared
- 'console/apps/switch-console-desktop/scripts/build-gateway-ui.mjs'
@@ -249,7 +250,7 @@ jobs:
console:
name: Switch Console
needs: changes
- if: ${{ needs.changes.outputs.console == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
+ if: ${{ needs.changes.outputs.console == 'true' || needs.changes.outputs.local_server_bundle == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
defaults:
run:
@@ -318,7 +319,7 @@ jobs:
console-windows:
name: Switch Console (Windows)
needs: changes
- if: ${{ needs.changes.outputs.console == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
+ if: ${{ needs.changes.outputs.console == 'true' || needs.changes.outputs.local_server_bundle == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
runs-on: windows-latest
defaults:
run:
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 fc30be63a..937c4928f 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,6 @@ import { buildNativeCoreSettings } from './core-settings';
import {
buildGatewayUi,
checkoutGatewayUi,
- downloadedGatewayUiWouldBuild,
GATEWAY_UI_ENV,
gatewayUiBuildFailed,
gatewayUiFailedMarker,
@@ -18,7 +17,7 @@ import {
runningServesAdminPages,
} from './gateway-ui';
import { nativeLayout } from './native-paths';
-import { installSwitchCore } from './switch-core-install';
+import { downloadedInstallWouldServePages, installSwitchCore } from './switch-core-install';
import { BUNDLE_FORMAT } from './toolchain-pins';
vi.mock('@main/lib/logger', () => ({
@@ -175,13 +174,15 @@ describe('building the pages from source', () => {
/** Puts an `npm` on PATH that records its arguments and, on `run build`,
* writes a build (or fails, with `fail`). */
- async function fakeNpm(fail = false): Promise<string> {
+ async function fakeNpm(fail: false | 'build' | 'ci' = false): Promise<string> {
const bin = path.join(root, 'bin');
const calls = path.join(root, 'npm-calls');
await writeExecutable(path.join(bin, 'npm'), [
`echo "$@ NODE_ENV=$NODE_ENV" >> "${calls}"`,
- 'if [ "$1" = ci ]; then mkdir -p node_modules; fi',
- fail
+ fail === 'ci'
+ ? 'if [ "$1" = ci ]; then echo "npm ERR! network ETIMEDOUT" >&2; exit 1; fi'
+ : 'if [ "$1" = ci ]; then mkdir -p node_modules; fi',
+ fail === 'build'
? 'if [ "$1" = run ]; then echo "vite: build failed" >&2; exit 1; fi'
: 'if [ "$1" = run ]; then mkdir -p dist && echo ok > dist/index.html; fi',
'exit 0',
@@ -221,7 +222,7 @@ describe('building the pages from source', () => {
});
unix('reports a failed build and carries on without the pages', async () => {
- await fakeNpm(true);
+ await fakeNpm('build');
const src = await source();
const messages: string[] = [];
const built = await buildGatewayUi({
@@ -242,16 +243,47 @@ describe('building the pages from source', () => {
expect(messages.at(-1)).toContain(gatewayUiFailedMarker(path.join(root, 'gateway-ui')));
});
+ unix('tries again next start after a failure that may not repeat', async () => {
+ await fakeNpm('ci');
+ const src = await source();
+ const messages: string[] = [];
+ const destination = path.join(root, 'gateway-ui');
+ expect(
+ await buildGatewayUi({
+ layout: nativeLayout(root),
+ sourceRoot: src,
+ destination,
+ onMessage: (message) => messages.push(message),
+ })
+ ).toBeNull();
+ expect(await gatewayUiBuildFailed(destination)).toBe(false);
+ expect(messages.at(-1)).toMatch(/next start tries again/);
+ });
+
+ it('offers no restart for an install from a bundle that shipped no pages', async () => {
+ const layout = nativeLayout(path.join(root, 'server'));
+ const dir = path.join(layout.switchCore, '0.31.0');
+ await mkdir(dir, { recursive: true });
+ await writeFile(
+ path.join(dir, 'installed.json'),
+ JSON.stringify({ version: '0.31.0', source: 'bundle' })
+ );
+ // Reused as it is by a dev build with no bundle, and never built for.
+ expect(await downloadedInstallWouldServePages(layout, '0.31.0')).toBe(false);
+ await writeBuild(path.join(dir, 'gateway-ui'));
+ expect(await downloadedInstallWouldServePages(layout, '0.31.0')).toBe(true);
+ });
+
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);
+ expect(await downloadedInstallWouldServePages(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);
+ expect(await downloadedInstallWouldServePages(layout, '0.31.0')).toBe(false);
+ expect(await downloadedInstallWouldServePages(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 0a076d159..66ea4eefb 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
@@ -27,24 +27,15 @@ 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.
+ * Left beside an install's pages when building them failed in a way that
+ * would repeat, 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 {
@@ -121,20 +112,29 @@ export async function buildGatewayUi(opts: {
timeoutMs: BUILD_TIMEOUT_MS,
});
};
+ // Only the build itself failing is recorded: it fails the same way on the
+ // same source every time. Installing the packages offline or against a
+ // registry error, npm missing from PATH, or a timeout may well pass next
+ // time, so the next start tries again.
+ let building = false;
try {
await appendFile(buildLog, `\n# build ${project} (${new Date().toISOString()})\n`);
await run(['ci', '--no-audit', '--no-fund']);
+ building = true;
await run(['run', 'build']);
await rm(destination, { recursive: true, force: true });
await rename(path.join(project, 'dist'), destination);
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
log.warn('local-switch-server: could not build the admin pages', { error: reason });
+ const repeats = building && /failed \(exit \d+\)/.test(reason);
const failed = gatewayUiFailedMarker(destination);
- await writeFile(failed, `${new Date().toISOString()} ${reason}\n`).catch(() => undefined);
+ if (repeats) {
+ 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.`
+ (repeats ? `Delete ${failed} and restart to try again.` : 'The next start tries again.')
);
return null;
} finally {
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 b7643031a..3ad528115 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, downloadedGatewayUiWouldBuild, nativeAdminPages } from './gateway-ui';
+import { checkoutGatewayUi, nativeAdminPages } from './gateway-ui';
import {
mattermostCallbackBaseUrl,
pullMattermostImage,
@@ -76,6 +76,7 @@ import {
writeClusterConfig,
} from './postgres-cluster';
import {
+ downloadedInstallWouldServePages,
existingSwitchCoreInstall,
installSwitchCore,
pruneSwitchCoreInstalls,
@@ -614,8 +615,8 @@ 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 — unless building them for
- * that version already failed, which a restart would not retry.
+ * builds) the install a start would use would have them built (see
+ * `downloadedInstallWouldServePages`).
*/
export async function nativeAdminPagesOnRestart(
layout: NativeLayout,
@@ -626,7 +627,7 @@ export async function nativeAdminPagesOnRestart(
const version = managedSwitchVersion();
const bundle = await readLocalServerBundle(bundleRoot, version);
if (bundle !== null) return bundle.gatewayUi !== null;
- return import.meta.env.DEV && (await downloadedGatewayUiWouldBuild(layout, version));
+ return import.meta.env.DEV && (await downloadedInstallWouldServePages(layout, version));
}
/** What the running native server's admin pages are doing, for its page to
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 1d5145e7a..ccb19cc42 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
@@ -106,6 +106,27 @@ async function readMarker(dir: string): Promise<Marker | null> {
}
}
+/**
+ * Whether a dev build with no bundle, starting switch-core `version`, would
+ * hand it the gateway's pages. An install of that version already there is
+ * reused as it is (see `markerMatches`): one made from a bundle has whatever
+ * the bundle shipped and is never built for, having no gateway source. A
+ * downloaded one, or none yet, gets its pages built, unless building them for
+ * it already failed in a way that would repeat.
+ */
+export async function downloadedInstallWouldServePages(
+ layout: NativeLayout,
+ version: string
+): Promise<boolean> {
+ const dir = path.join(layout.switchCore, version);
+ const gatewayUi = path.join(dir, GATEWAY_UI_DIR_NAME);
+ const marker = await readMarker(dir);
+ if (marker?.version === version && marker.source === 'bundle') {
+ return (await gatewayUiOrNull(gatewayUi)) !== null;
+ }
+ return !(await gatewayUiBuildFailed(gatewayUi));
+}
+
/** Whether an install made from `marker` serves `version` from `bundle`: one
* made from another copy of the bundle is redone, so old copies can go. */
function markerMatches(