From 6e587a910de548e2273e296f4611675c2edbcd4c Mon Sep 17 00:00:00 2001 From: cxxxxxn Date: Mon, 19 Oct 2026 14:05:12 +0800 Subject: [PATCH] fix(desktop): bundle service resources without test sources Include Service manifests and runtime files in desktop packages, add an isolated bundle regression test, and document the packaging contract. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- apps/desktop/electron-builder.yml | 6 ++ apps/server/src/bundle.test.ts | 89 ++++++++++++++++++++++++++- docs/architecture/service-packages.md | 2 + 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/apps/desktop/electron-builder.yml b/apps/desktop/electron-builder.yml index c387a13c8..9e4461296 100644 --- a/apps/desktop/electron-builder.yml +++ b/apps/desktop/electron-builder.yml @@ -76,6 +76,12 @@ extraResources: # picks up sibling `.ts` source files too; exclude them so we don't # bloat the installer with dead source. - '**/*.md' + # Service manifests and their declared files are read during server startup. + - from: ../server/dist-bundle/services + to: server/services + filter: + - '**/*' + - '!**/*.test.*' - from: ../server/dist-bundle/agentlet to: server/agentlet filter: diff --git a/apps/server/src/bundle.test.ts b/apps/server/src/bundle.test.ts index be6b43a0a..588ec225c 100644 --- a/apps/server/src/bundle.test.ts +++ b/apps/server/src/bundle.test.ts @@ -3,7 +3,14 @@ import { execFileSync, spawn } from 'node:child_process'; import { once } from 'node:events'; -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { + cpSync, + mkdtempSync, + readFileSync, + readdirSync, + rmSync, + writeFileSync, +} from 'node:fs'; import { createServer } from 'node:http'; import { tmpdir } from 'node:os'; import path from 'node:path'; @@ -12,6 +19,7 @@ import { pathToFileURL } from 'node:url'; import archiver from 'archiver'; import { build } from 'tsup'; import { describe, expect, it } from 'vitest'; +import { parse } from 'yaml'; import { buildHuabuBootstrapEnv, @@ -19,12 +27,91 @@ import { } from '../../../packages/client-runtime/src/bootstrap.js'; import clientRuntimeConfig from '../../../packages/client-runtime/tsup.config.js'; import bundleConfig from '../tsup.config.js'; +import { getBundledServicePackages } from './modules/services/package-loader.js'; const configured = typeof bundleConfig === 'function' ? await bundleConfig({}) : bundleConfig; const configurations = Array.isArray(configured) ? configured : [configured]; describe('server bundle runtime', () => { + it('ships and loads every Service package in the desktop resource layout', async () => { + const desktopConfig = parse( + readFileSync(path.resolve('../desktop/electron-builder.yml'), 'utf8'), + ); + const serviceResource = desktopConfig.extraResources.find( + (resource: { from: string }) => + resource.from === '../server/dist-bundle/services', + ); + expect(serviceResource).toEqual({ + from: '../server/dist-bundle/services', + to: 'server/services', + filter: ['**/*', '!**/*.test.*'], + }); + + const directory = mkdtempSync(path.join(tmpdir(), 'huabu-services-')); + try { + const resources = path.join(directory, 'resources'); + const sourceRoot = path.resolve('src/services'); + const packagedRoot = path.join(resources, serviceResource.to); + const exclusions: string[] = serviceResource.filter + .filter((pattern: string) => pattern.startsWith('!')) + .map((pattern: string) => pattern.slice(1)); + cpSync(sourceRoot, packagedRoot, { + recursive: true, + filter: (source) => + !exclusions.some((pattern) => + path.matchesGlob(path.relative(sourceRoot, source), pattern), + ), + }); + const isTestFile = (file: string) => + path.matchesGlob(file, '**/*.test.*'); + expect( + readdirSync(sourceRoot, { + recursive: true, + encoding: 'utf8', + }).some(isTestFile), + ).toBe(true); + expect( + readdirSync(packagedRoot, { + recursive: true, + encoding: 'utf8', + }).some(isTestFile), + ).toBe(false); + const entry = path.join(resources, 'server/server.mjs'); + await build({ + ...configurations[0], + config: false, + entry: { + server: path.resolve('src/modules/services/package-loader.ts'), + }, + outDir: path.dirname(entry), + outExtension: () => ({ js: '.mjs' }), + onSuccess: undefined, + silent: true, + }); + const summarize = + '(m) => [...m.getBundledServicePackages()].map(([id, p]) => [id, p.contentHash])'; + const output = execFileSync( + process.execPath, + [ + '--input-type=module', + '-e', + `const m = await import(process.argv[1]); console.log(JSON.stringify((${summarize})(m)));`, + pathToFileURL(entry).href, + ], + { cwd: directory, encoding: 'utf8' }, + ); + expect(JSON.parse(output)).toEqual( + [...getBundledServicePackages()].map(([id, service]) => [ + id, + service.contentHash, + ]), + ); + } finally { + rmSync(directory, { recursive: true, force: true }); + } + }); + it('loads real Agentlet and client-runtime artifacts outside the workspace without node_modules', async () => { const directory = mkdtempSync(path.join(tmpdir(), 'huabu-real-runtime-')); try { diff --git a/docs/architecture/service-packages.md b/docs/architecture/service-packages.md index c4d8b31f7..e0ee4d463 100644 --- a/docs/architecture/service-packages.md +++ b/docs/architecture/service-packages.md @@ -17,6 +17,8 @@ The bundled packages live under `apps/server/src/services//`: Phase 1 packages are trusted files shipped with Huabu. User upload, installation, remote registries, and Server execution of uploaded code are not supported. +Server bundling copies the Service tree to `apps/server/dist-bundle/services/`. Desktop packaging includes that tree at `resources/server/services/`, alongside `server.js`, excluding `*.test.*` source files while retaining every `service.yaml` and its declared runtime Package files. The loader validates these files during module initialization, so missing resources prevent the local Server from starting even when no Service has been configured. The bundle runtime regression test verifies the desktop resource mapping, excludes test sources, and loads the packages from an isolated packaged layout without the source-tree fallback. + ## Manifest contract `huabu-service/v1` is defined by `serviceManifestSchema` in `packages/shared/src/types/api/service-package.ts`. Unknown properties, malformed IDs, duplicate configuration IDs, invalid enum declarations, ambiguous Package paths, duplicate Package files, and `entry.mjs` without `SKILL.md` fail validation.