Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Fix bundled skill installation from ASAR (PRD-9697) (#7094)
tim-inkeep5ea95f0811d3
diff --git a/.changeset/prd-9697-asar-project-skill.md b/.changeset/prd-9697-asar-project-skill.mdnew file mode 100644index 000000000..29a4d39d3--- /dev/null+++ b/.changeset/prd-9697-asar-project-skill.md@@ -0,0 +1,6 @@+---+"@inkeep/open-knowledge": patch+"@inkeep/open-knowledge-desktop": patch+---++Fix installing bundled project and user skills from the desktop app, including Pi on Windows.diff --git a/packages/cli/src/integrations/replace-bundle-dir.ts b/packages/cli/src/integrations/replace-bundle-dir.tsnew file mode 100644index 000000000..4a02fba02--- /dev/null+++ b/packages/cli/src/integrations/replace-bundle-dir.ts@@ -0,0 +1,29 @@+import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs';+import { dirname, join } from 'node:path';++export function replaceBundleDir(sourceDir: string, skillPath: string): 'written' | 'overwritten' {+ const targetDir = dirname(skillPath);+ const action = existsSync(skillPath) ? 'overwritten' : 'written';+ if (action === 'written' && existsSync(targetDir)) {+ throw new Error(+ `Refusing to replace ${targetDir}: it exists but holds no SKILL.md, so it is not a bundle OpenKnowledge wrote. Move it aside and try again.`,+ );+ }+ rmSync(targetDir, { recursive: true, force: true });+ mkdirSync(dirname(targetDir), { recursive: true });+ copyBundleContents(sourceDir, targetDir);+ return action;+}++function copyBundleContents(sourceDir: string, targetDir: string): void {+ mkdirSync(targetDir, { recursive: true });+ for (const entry of readdirSync(sourceDir, { withFileTypes: true })) {+ const source = join(sourceDir, entry.name);+ const target = join(targetDir, entry.name);+ if (entry.isDirectory()) {+ copyBundleContents(source, target);+ } else {+ copyFileSync(source, target);+ }+ }+}diff --git a/packages/cli/src/integrations/write-project-skill.ts b/packages/cli/src/integrations/write-project-skill.tsindex 4de97d2f5..8f1285075 100644--- a/packages/cli/src/integrations/write-project-skill.ts+++ b/packages/cli/src/integrations/write-project-skill.ts@@ -1,13 +1,4 @@-import {- cpSync,- existsSync,- lstatSync,- mkdirSync,- readFileSync,- realpathSync,- rmSync,- statSync,-} from 'node:fs';+import { existsSync, lstatSync, readFileSync, realpathSync, rmSync, statSync } from 'node:fs';import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';import {AGENT_REGISTRY,@@ -26,6 +17,7 @@ import {entryCarriesOwnChain,isOwnDevEntry,} from '../commands/editors.ts';+import { replaceBundleDir } from './replace-bundle-dir.ts';export class ProjectPathSafetyError extends Error {override readonly name = 'ProjectPathSafetyError';@@ -88,20 +80,6 @@ export function assertAncestorsContainedIn(targetPath: string, base: string): vo}}-export function replaceBundleDir(sourceDir: string, skillPath: string): 'written' | 'overwritten' {- const targetDir = dirname(skillPath);- const action = existsSync(skillPath) ? 'overwritten' : 'written';- if (action === 'written' && existsSync(targetDir)) {- throw new Error(- `Refusing to replace ${targetDir}: it exists but holds no SKILL.md, so it is not a bundle OpenKnowledge wrote. Move it aside and try again.`,- );- }- rmSync(targetDir, { recursive: true, force: true });- mkdirSync(dirname(targetDir), { recursive: true });- cpSync(sourceDir, targetDir, { recursive: true });- return action;-}-export interface ProjectSkillResult {readonly editorId: EditorId;readonly label: string;diff --git a/packages/cli/src/integrations/write-user-skill.ts b/packages/cli/src/integrations/write-user-skill.tsindex a082303bf..0ee92e145 100644--- a/packages/cli/src/integrations/write-user-skill.ts+++ b/packages/cli/src/integrations/write-user-skill.ts@@ -11,11 +11,11 @@ import {resolveBundledSkillDir,} from '@inkeep/open-knowledge-server';import type { EditorId } from '../commands/editors.ts';+import { replaceBundleDir } from './replace-bundle-dir.ts';import {assertAncestorsContainedIn,type ProjectSkillRemoveResult,type ProjectSkillResult,- replaceBundleDir,} from './write-project-skill.ts';function userSkillPath(editor: EditorId, home: string): string | null {diff --git a/packages/desktop/tests/smoke/_helpers/platform-gate.ts b/packages/desktop/tests/smoke/_helpers/platform-gate.tsindex 0d72b0e30..e09c520d2 100644--- a/packages/desktop/tests/smoke/_helpers/platform-gate.ts+++ b/packages/desktop/tests/smoke/_helpers/platform-gate.ts@@ -44,6 +44,7 @@ export const SPEC_PLATFORM_GATES = {'share-receive-miss-terminal.e2e.ts': ['!PLATFORM_SUPPORTED'],'share-receive-multi-worktree.e2e.ts': ['!DARWIN'],'sidebar-create-rename-editable.e2e.ts': ['!DARWIN'],+ 'skill-bundle-asar.e2e.ts': [],'skill-scope-roundtrip.e2e.ts': ['!DARWIN'],'skills-studio.e2e.ts': ['!DARWIN'],'slidev-render-readiness.e2e.ts': ['!PLATFORM_SUPPORTED'],diff --git a/packages/desktop/tests/smoke/skill-bundle-asar.e2e.ts b/packages/desktop/tests/smoke/skill-bundle-asar.e2e.tsnew file mode 100644index 000000000..6bda33388--- /dev/null+++ b/packages/desktop/tests/smoke/skill-bundle-asar.e2e.ts@@ -0,0 +1,76 @@+import { spawnSync } from 'node:child_process';+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { stripTypeScriptTypes } from 'node:module';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { expect, test } from '@playwright/test';+import electron from 'electron';++test('PRD-9697: installs and replaces a bundled skill from a real ASAR', () => {+ const root = mkdtempSync(join(tmpdir(), 'ok-skill-asar-'));+ try {+ const skill = Buffer.from('---\nname: open-knowledge\n---\nBundled skill\n');+ const asset = Buffer.from([0, 255, 13, 10, 128]);+ const json = Buffer.from(+ JSON.stringify({+ files: {+ project: {+ files: {+ 'SKILL.md': { size: skill.length, offset: '0' },+ references: {+ files: { 'asset.bin': { size: asset.length, offset: String(skill.length) } },+ },+ },+ },+ },+ }),+ );+ const headerSize = Math.ceil((json.length + 4) / 4) * 4;+ const header = Buffer.alloc(12 + headerSize);+ header.writeUInt32LE(4, 0);+ header.writeUInt32LE(headerSize + 4, 4);+ header.writeUInt32LE(headerSize, 8);+ header.writeUInt32LE(json.length, 12);+ json.copy(header, 16);+ writeFileSync(join(root, 'app.asar'), Buffer.concat([header, skill, asset]));+ const source = readFileSync(+ new URL('../../../cli/src/integrations/replace-bundle-dir.ts', import.meta.url),+ 'utf8',+ );+ writeFileSync(join(root, 'copy.mjs'), stripTypeScriptTypes(source));+ writeFileSync(+ join(root, 'main.cjs'),+ `+ const { app } = require('electron');+ const fs = require('node:fs');+ const path = require('node:path');+ const assert = require('node:assert/strict');+ const fixtureRoot = path.dirname(process.argv[1]);+ (async () => {+ const { replaceBundleDir } = await import('./copy.mjs');+ const source = path.join(fixtureRoot, 'app.asar', 'project');+ assert.deepEqual(fs.readFileSync(path.join(source, 'SKILL.md')), Buffer.from(${JSON.stringify([...skill])}));+ const target = path.join(fixtureRoot, 'project', '.pi', 'skills', 'open-knowledge', 'SKILL.md');+ assert.equal(replaceBundleDir(source, target), 'written');+ fs.writeFileSync(path.join(path.dirname(target), 'stale.txt'), 'old');+ assert.equal(replaceBundleDir(source, target), 'overwritten');+ assert.equal(fs.existsSync(path.join(path.dirname(target), 'stale.txt')), false);+ assert.deepEqual(fs.readFileSync(target), Buffer.from(${JSON.stringify([...skill])}));+ assert.deepEqual(fs.readFileSync(path.join(path.dirname(target), 'references', 'asset.bin')), Buffer.from(${JSON.stringify([...asset])}));+ app.exit(0);+ })().catch(error => { process.stderr.write(String(error.stack)); app.exit(1); });+ `,+ );+ const env = { ...process.env };+ delete env.ELECTRON_RUN_AS_NODE;+ const result = spawnSync(String(electron), [join(root, 'main.cjs')], {+ env,+ encoding: 'utf8',+ timeout: 20_000,+ });+ expect(result.error, result.stderr).toBeUndefined();+ expect(result.status, result.stderr).toBe(0);+ } finally {+ rmSync(root, { recursive: true, force: true });+ }+});