Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Read bundled skill sources when the server runs from source (#5112)
Nick Gomez87ca54504dbc
diff --git a/packages/server/scripts/build-skill-bundles.test.ts b/packages/server/scripts/build-skill-bundles.test.tsindex 91f95191b..7bc397c08 100644--- a/packages/server/scripts/build-skill-bundles.test.ts+++ b/packages/server/scripts/build-skill-bundles.test.ts@@ -438,7 +438,7 @@ describe('repo assets — the source-fallback tripwire', () => {.map((file) => relative(skillsDir, file));expect(referencing,- 'resolveBundledSkillDir probes dist/assets/skills/<which> and then falls through to the uncomposed assets/skills/<which> (build-skill-zip.ts:51-52), and listPackSkillSources resolves pack directories through that same chain, so a reader arriving while a publish is between its two renames receives SOURCE bytes. That is harmless only while composition is an identity transform. The first SKILL.md to reference a shared include makes that fallback serve an unresolved placeholder, silently, and no other check in this repo reds on it. This test pins the condition that keeps the residual absent window benign; retire it by making the fallback unable to serve uncomposed bytes, not by deleting it.',+ 'resolveBundledSkillDir reads the uncomposed assets/skills/<which> whenever it runs from source, and from a build it probes dist/assets/skills/<which> before falling through to that same uncomposed source (bundledSkillCandidates, build-skill-zip.ts); listPackSkillSources resolves pack directories through that resolver. So every source-mode reader, and any built reader arriving while a publish is between its two renames, receives SOURCE bytes. That is harmless only while composition is an identity transform. The first SKILL.md to reference a shared include makes those readers serve an unresolved placeholder, silently, and no other check in this repo reds on it. This test pins that condition; retire it by making every reader unable to serve uncomposed bytes, not by deleting it.',).toEqual([]);});});diff --git a/packages/server/scripts/build-skill-bundles.ts b/packages/server/scripts/build-skill-bundles.tsindex e70f7317a..eac6b040b 100644--- a/packages/server/scripts/build-skill-bundles.ts+++ b/packages/server/scripts/build-skill-bundles.ts@@ -13,8 +13,11 @@** packages/server/dist/assets/skills/<bundle>/SKILL.md*- * `resolveBundledSkillDir()` probes that dist path before the source path, so- * the composed (placeholder-free) bundle is what gets installed. At v1+ * A built `resolveBundledSkillDir()` (the CLI bundle, the server build the+ * desktop app loads) probes that dist path before the source path, so built+ * artifacts install the composed (placeholder-free) bundle; run from source it+ * reads the uncomposed source directly, which the source-fallback tripwire in+ * build-skill-bundles.test.ts keeps safe. At v1* `_shared/` carries no shared prose yet — neither bundle references a* placeholder, so composition is an identity transform — but the mechanism +* the byte-equality guard exist now so shared prose can land without driftdiff --git a/packages/server/src/build-skill-zip.layout.test.ts b/packages/server/src/build-skill-zip.layout.test.tsnew file mode 100644index 000000000..ea5327ffe--- /dev/null+++ b/packages/server/src/build-skill-zip.layout.test.ts@@ -0,0 +1,118 @@+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { fileURLToPath, pathToFileURL } from 'node:url';+import { afterEach, describe, expect, test, vi } from 'vitest';++vi.mock('node:fs', async (importOriginal) => {+ const actual = await importOriginal<typeof import('node:fs')>();+ const buildOutput = fileURLToPath(new URL('../dist/', import.meta.url));+ return {+ ...actual,+ existsSync: (path: Parameters<typeof actual.existsSync>[0]) =>+ String(path).startsWith(buildOutput) || actual.existsSync(path),+ };+});++import { __testing, resolveBundledSkillDir } from './build-skill-zip.ts';+import { listPackSkillSources } from './skill-pack-sources.ts';++const { bundledSkillCandidates, missingBundleMessage } = __testing;+const PACKAGE = fileURLToPath(new URL('..', import.meta.url));+const APP_SKILLS = join(+ 'OpenKnowledge.app',+ 'Contents',+ 'Resources',+ 'cli',+ 'dist',+ 'assets',+ 'skills',+);+const ROOT = join(tmpdir(), 'ok-bundled-skill-layout');+const moduleAt = (...segments: string[]) => pathToFileURL(join(ROOT, ...segments)).href;+const at = (...segments: string[]) => join(ROOT, ...segments);++describe('resolveBundledSkillDir running from source', () => {+ test('reads the package source assets even when a build output is present', () => {+ expect(resolveBundledSkillDir('project')).toBe(join(PACKAGE, 'assets', 'skills', 'project'));+ expect(listPackSkillSources('okf')[0]?.sourceDir).toBe(+ join(PACKAGE, 'assets', 'skills', 'packs', 'okf'),+ );+ });++ test('a bundle missing from the source stays missing even when a build output holds it', () => {+ expect(() => resolveBundledSkillDir('packs/removed-pack')).toThrow(/not found/);+ expect(listPackSkillSources('removed-pack')).toEqual([]);+ });++ test('a miss names the source assets and prescribes no build', () => {+ expect(() => resolveBundledSkillDir('no-such-bundle')).toThrow(+ join(PACKAGE, 'assets', 'skills', 'no-such-bundle'),+ );+ expect(() => resolveBundledSkillDir('no-such-bundle')).not.toThrow(/pnpm run build/);+ });+});++describe('missingBundleMessage', () => {+ test('a miss from a build prescribes the package build', () => {+ const tried = [at('cli', 'dist', 'assets', 'skills', 'project')];+ expect(missingBundleMessage('project', moduleAt('cli', 'dist', 'cli.mjs'), tried)).toContain(+ 'Run `pnpm run build`',+ );+ expect(+ missingBundleMessage('project', moduleAt('pkg', 'src', 'build-skill-zip.ts'), tried),+ ).not.toContain('pnpm run build');+ });+});++describe('bundledSkillCandidates', () => {+ afterEach(() => {+ vi.unstubAllEnvs();+ });++ test('a module running from source probes only the package source assets', () => {+ expect(+ bundledSkillCandidates('project', moduleAt('pkg', 'src', 'build-skill-zip.ts'), {}),+ ).toEqual([at('pkg', 'assets', 'skills', 'project')]);+ });++ test('a query on the module URL leaves it running from source', () => {+ const url = `${moduleAt('pkg', 'src', 'build-skill-zip.ts')}?t=1`;+ expect(bundledSkillCandidates('project', url, {})).toEqual([+ at('pkg', 'assets', 'skills', 'project'),+ ]);+ });++ test('the server build installed with the desktop app probes its build output first', () => {+ const server = ['app', 'node_modules', '@inkeep', 'open-knowledge-server'];+ expect(+ bundledSkillCandidates('packs/okf', moduleAt(...server, 'dist', 'index.mjs'), {}),+ ).toEqual([+ at(...server, 'dist', 'assets', 'skills', 'packs', 'okf'),+ at(...server, 'assets', 'skills', 'packs', 'okf'),+ at(...server, 'dist', 'assets', 'skills', 'packs', 'okf'),+ ]);+ });++ test('the CLI bundle probes the assets beside it first', () => {+ expect(bundledSkillCandidates('discovery', moduleAt('cli', 'dist', 'cli.mjs'), {})).toEqual([+ at('cli', 'dist', 'assets', 'skills', 'discovery'),+ at('cli', 'assets', 'skills', 'discovery'),+ at('cli', 'dist', 'assets', 'skills', 'discovery'),+ ]);+ });++ test('a co-installed desktop app comes first in both layouts, /Applications skipped under test', () => {+ vi.stubEnv('NODE_ENV', 'test');+ const opts = { checkDesktop: true, platform: 'darwin' as const, home: at('home') };+ const desktop = join(at('home'), 'Applications', APP_SKILLS, 'project');+ expect(+ bundledSkillCandidates('project', moduleAt('pkg', 'src', 'build-skill-zip.ts'), opts),+ ).toEqual([desktop, at('pkg', 'assets', 'skills', 'project')]);+ expect(bundledSkillCandidates('project', moduleAt('cli', 'dist', 'cli.mjs'), opts)).toEqual([+ desktop,+ at('cli', 'dist', 'assets', 'skills', 'project'),+ at('cli', 'assets', 'skills', 'project'),+ at('cli', 'dist', 'assets', 'skills', 'project'),+ ]);+ });+});diff --git a/packages/server/src/build-skill-zip.ts b/packages/server/src/build-skill-zip.tsindex 443e61301..c2074daad 100644--- a/packages/server/src/build-skill-zip.ts+++ b/packages/server/src/build-skill-zip.ts@@ -32,10 +32,15 @@ export interface ResolveBundledSkillDirOptions {const DESKTOP_SKILLS_REL = 'OpenKnowledge.app/Contents/Resources/cli/dist/assets/skills';-export function resolveBundledSkillDir(- which: BundleId | (string & {}),- opts: ResolveBundledSkillDirOptions = {},-): string {+function runsFromSource(moduleUrl: string): boolean {+ return new URL(moduleUrl).pathname.endsWith('.ts');+}++function bundledSkillCandidates(+ which: string,+ moduleUrl: string,+ opts: ResolveBundledSkillDirOptions,+): string[] {const platform = opts.platform ?? process.platform;const checkDesktop = opts.checkDesktop ?? false;const home = opts.home ?? homedir();@@ -48,19 +53,32 @@ export function resolveBundledSkillDir(candidates.push(join(home, 'Applications', DESKTOP_SKILLS_REL, which));}}- candidates.push(fileURLToPath(new URL(`../dist/assets/skills/${which}`, import.meta.url)));- candidates.push(fileURLToPath(new URL(`../assets/skills/${which}`, import.meta.url)));- candidates.push(fileURLToPath(new URL(`./assets/skills/${which}`, import.meta.url)));+ if (runsFromSource(moduleUrl)) {+ candidates.push(fileURLToPath(new URL(`../assets/skills/${which}`, moduleUrl)));+ return candidates;+ }+ candidates.push(fileURLToPath(new URL(`../dist/assets/skills/${which}`, moduleUrl)));+ candidates.push(fileURLToPath(new URL(`../assets/skills/${which}`, moduleUrl)));+ candidates.push(fileURLToPath(new URL(`./assets/skills/${which}`, moduleUrl)));+ return candidates;+}++function missingBundleMessage(which: string, moduleUrl: string, candidates: string[]): string {+ const remedy = runsFromSource(moduleUrl)+ ? 'Running from source, only the source assets are read, so the bundle name is unknown or its directory under packages/server/assets/skills is missing.'+ : 'This usually means the package build did not copy packages/server/assets into dist/assets. Run `pnpm run build` in the package that ships the skills before publishing.';+ return `Bundled skill asset directory not found for bundle '${which}'. Tried: ${candidates.join(', ')}. ${remedy}`;+}+export function resolveBundledSkillDir(+ which: BundleId | (string & {}),+ opts: ResolveBundledSkillDirOptions = {},+): string {+ const candidates = bundledSkillCandidates(which, import.meta.url, opts);for (const candidate of candidates) {if (existsSync(candidate)) return candidate;}- throw new Error(- `Bundled skill asset directory not found for bundle '${which}'. ` +- `Tried: ${candidates.join(', ')}. ` +- 'This usually means the CLI build did not copy packages/server/assets into dist/assets. ' +- 'Run `cd packages/cli && bun run build` before publishing.',- );+ throw new Error(missingBundleMessage(which, import.meta.url, candidates));}async function* walkFiles(dir: string, base: string = dir): AsyncGenerator<string> {@@ -163,4 +181,9 @@ export async function buildSkillZip(opts: BuildSkillZipOptions = {}): Promise<Bureturn { outputPath, size, sha256 };}-export const __testing = { computeWrapperFolderName, toPosixZipPath };+export const __testing = {+ computeWrapperFolderName,+ toPosixZipPath,+ bundledSkillCandidates,+ missingBundleMessage,+};diff --git a/packages/server/vitest.config.ts b/packages/server/vitest.config.tsindex 1a0ecd810..fa44c2e72 100644--- a/packages/server/vitest.config.ts+++ b/packages/server/vitest.config.ts@@ -5,6 +5,6 @@ export default defineConfig({...okVitestBase,test: {...okVitestBase.test,- exclude: [...okVitestBase.test.exclude, '**/*.network.test.ts'],+ exclude: [...okVitestBase.test.exclude, '**/*.network.test.ts', '**/dist/**'],},});