|
1 | 1 | // Skills directory resolver. |
2 | 2 | // |
3 | | -// Two packaging modes: |
| 3 | +// Materializes the skills tree to `<dataDir>/skills/` and substitutes the |
| 4 | +// `{{SKILLS_DIR}}` placeholder in every file with that absolute path so |
| 5 | +// cross-references inside BROWSER.md (``read `{{SKILLS_DIR}}/cloud-browser.md` ``) |
| 6 | +// point at a real location. |
4 | 7 | // |
5 | | -// 1. Dev mode — `import.meta.url` resolves to `packages/bcode-browser/src/` |
6 | | -// on disk, skills live at the sibling `../skills/`. Used by `bun run |
7 | | -// --cwd packages/opencode dev` and tests. |
| 8 | +// Compiled launches (the user-facing path) read a one-line sentinel at |
| 9 | +// `<target>/.bcode-build` recording `<buildHash>:<target>`. When it matches |
| 10 | +// — i.e. same build, same dataDir — the resolver returns immediately |
| 11 | +// without reading or writing any skill content. The build hash is computed |
| 12 | +// once by `script/embed-skills.ts` and lives in the binary, so the cost is |
| 13 | +// a single small file read. |
8 | 14 | // |
9 | | -// 2. Compiled mode — running from a `bun build --compile` binary. |
10 | | -// `import.meta.dir` lives under `/$bunfs/` (or `B:/~BUN/` on Windows), |
11 | | -// a read-only virtual filesystem the agent's `read` tool can't see in a |
12 | | -// useful path shape. We extract the embedded skills (built into the |
13 | | -// binary by `script/embed-skills.ts`) to `<dataDir>/skills/`. A content- |
14 | | -// hash sentinel at `<dataDir>/skills/.bcode-build` records the embed |
15 | | -// bundle that produced the on-disk tree; warm launches stat-and-skip. |
16 | | -// |
17 | | -// Skills are read-only baseline: every launch overwrites the on-disk tree |
18 | | -// from the binary's embed (no agent-editable surface). The agent's editable |
19 | | -// surface is `<projectDir>/.bcode/agent-workspace/`, per-project, never here. |
| 15 | +// Dev launches (`bun run dev`) always re-extract from the worktree so |
| 16 | +// editor saves to source skill files land on the next launch without a |
| 17 | +// separate invalidation step. |
20 | 18 |
|
21 | 19 | import fs from "fs/promises" |
22 | 20 | import path from "path" |
23 | 21 | import { fileURLToPath } from "url" |
24 | 22 |
|
25 | 23 | const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
26 | | -const isCompiled = (() => { |
27 | | - const d = __dirname.replaceAll("\\", "/") |
28 | | - return d.startsWith("/$bunfs/") || d.startsWith("B:/~BUN/") |
29 | | -})() |
| 24 | +const isCompiled = __dirname.replaceAll("\\", "/").match(/^\/\$bunfs\/|^B:\/~BUN\//) !== null |
30 | 25 | const DEV_SKILLS_DIR = path.resolve(__dirname, "..", "skills") |
31 | | -const SENTINEL_NAME = ".bcode-build" |
| 26 | +const SENTINEL = ".bcode-build" |
32 | 27 |
|
33 | | -// Static path so the agent permission glob can use a stable absolute path. |
| 28 | +// Static — the agent permission glob and the substituted placeholder both |
| 29 | +// resolve to this path. |
34 | 30 | export const skillsDir = (dataDir: string) => path.join(dataDir, "skills") |
35 | 31 |
|
36 | | -const readSentinel = async (dir: string) => { |
37 | | - try { return await fs.readFile(path.join(dir, SENTINEL_NAME), "utf8") } |
38 | | - catch { return null } |
| 32 | +const cache = new Map<string, Promise<string>>() |
| 33 | + |
| 34 | +export const resolveSkillsDir = (dataDir: string): Promise<string> => { |
| 35 | + const cached = cache.get(dataDir) |
| 36 | + if (cached) return cached |
| 37 | + const fresh = materialize(skillsDir(dataDir)) |
| 38 | + cache.set(dataDir, fresh) |
| 39 | + fresh.catch(() => { if (cache.get(dataDir) === fresh) cache.delete(dataDir) }) |
| 40 | + return fresh |
39 | 41 | } |
40 | 42 |
|
41 | | -const extractEmbeddedSkills = async (dataDir: string): Promise<string> => { |
42 | | - const target = skillsDir(dataDir) |
| 43 | +const materialize = async (target: string): Promise<string> => { |
| 44 | + // Compiled-mode short-circuit: import the embed (cheap — just file |
| 45 | + // handles, no content read), check the sentinel, return on hit. |
43 | 46 | // @ts-expect-error generated at build time |
44 | | - const mod = await import("bcode-skills.gen.ts").catch(() => null) |
45 | | - if (!mod) throw new Error("bcode-skills.gen.ts not found in compiled binary — was the build script updated?") |
46 | | - const fileMap = mod.default as Record<string, string> |
47 | | - const buildHash = mod.buildHash as string |
48 | | - |
49 | | - if ((await readSentinel(target)) === buildHash) return target |
| 47 | + const embed = isCompiled ? await import("bcode-skills.gen.ts").catch(() => null) : null |
| 48 | + if (isCompiled && !embed) throw new Error("bcode-skills.gen.ts not found — was the build script updated?") |
| 49 | + const want = `${embed?.buildHash ?? "dev"}:${target}` |
| 50 | + if (embed && (await Bun.file(path.join(target, SENTINEL)).text().catch(() => null)) === want) return target |
50 | 51 |
|
| 52 | + const files = embed |
| 53 | + ? await readEmbed(embed.default as Record<string, string>) |
| 54 | + : await readDevSkills() |
51 | 55 | await fs.mkdir(target, { recursive: true }) |
52 | | - // Skills are baseline-overwrite — every file from the embed lands on disk. |
53 | 56 | await Promise.all( |
54 | | - Object.entries(fileMap).map(async ([rel, bunfsPath]) => { |
| 57 | + Object.entries(files).map(async ([rel, text]) => { |
55 | 58 | const dest = path.join(target, rel) |
56 | 59 | await fs.mkdir(path.dirname(dest), { recursive: true }) |
57 | | - await Bun.write(dest, Bun.file(bunfsPath)) |
| 60 | + await fs.writeFile(dest, text.replaceAll("{{SKILLS_DIR}}", target), "utf8") |
58 | 61 | }), |
59 | 62 | ) |
60 | | - await fs.writeFile(path.join(target, SENTINEL_NAME), buildHash, "utf8") |
| 63 | + if (embed) await fs.writeFile(path.join(target, SENTINEL), want, "utf8") |
61 | 64 | return target |
62 | 65 | } |
63 | 66 |
|
64 | | -const extractCache = new Map<string, Promise<string>>() |
| 67 | +const readEmbed = async (map: Record<string, string>): Promise<Record<string, string>> => |
| 68 | + Object.fromEntries( |
| 69 | + await Promise.all(Object.entries(map).map(async ([rel, p]) => [rel, await Bun.file(p).text()])), |
| 70 | + ) |
65 | 71 |
|
66 | | -export const resolveSkillsDir = (dataDir: string): Promise<string> => { |
67 | | - if (!isCompiled) return Promise.resolve(DEV_SKILLS_DIR) |
68 | | - const cached = extractCache.get(dataDir) |
69 | | - if (cached) return cached |
70 | | - const fresh = extractEmbeddedSkills(dataDir) |
71 | | - extractCache.set(dataDir, fresh) |
72 | | - fresh.catch(() => { |
73 | | - if (extractCache.get(dataDir) === fresh) extractCache.delete(dataDir) |
74 | | - }) |
75 | | - return fresh |
| 72 | +const readDevSkills = async (): Promise<Record<string, string>> => { |
| 73 | + const rels = await Array.fromAsync(new Bun.Glob("**/*").scan({ cwd: DEV_SKILLS_DIR })) |
| 74 | + return Object.fromEntries( |
| 75 | + await Promise.all( |
| 76 | + rels.map(async (rel) => [ |
| 77 | + rel.replaceAll("\\", "/"), |
| 78 | + await fs.readFile(path.join(DEV_SKILLS_DIR, rel), "utf8"), |
| 79 | + ]), |
| 80 | + ), |
| 81 | + ) |
76 | 82 | } |
77 | 83 |
|
78 | 84 | export * as Skills from "./skills" |
0 commit comments