import { assert, assertEquals, assertStringIncludes } from "@std/assert"; import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context"; import { getCurrentSystemPrompt } from "@earendil-works/pi-ai/utils/transcript"; import { fauxAssistantMessage, fauxToolCall } from "@earendil-works/pi-ai/providers/faux"; import { rootedEnvironment } from "@berry/api/testing.ts"; import { tempDir } from "@berry/util"; import { skillsExtension } from "../extensions/skills/mod.ts"; import { hostEnvironment, writeSkill } from "../extensions/skills/testing.ts"; import { lastToolResult, setup, toolsOffered, turn } from "./fixture.ts"; Deno.test("skill snapshots reach the next request and survive compaction, reload, and historical forks", async () => { await using root = await tempDir(); const env = rootedEnvironment(root.path); const skills = skillsExtension(hostEnvironment(root.path)); const body = "Review the primary evidence verbatim: ΞΆ exact spacing.\nNever skip the release checklist.\n"; await writeSkill(env, "/home/.agents/skills", "release", body); await using harness = await setup({ open: (name, cwd) => Promise.resolve(Object.assign(rootedEnvironment(root.path, { cwd: cwd ?? "/" }), { name })), close: () => Promise.resolve(), }); const { berry, model, faux } = harness; await berry.install(skills); const tid = await berry.create({ extensions: { environment: { cwd: "/" } }, model }); const conversation = (await berry.conversation(tid))!; const [before, after] = await turn(harness, tid, [ fauxAssistantMessage(fauxToolCall("load_skill", { name: "release" }), { stopReason: "toolUse", }), ]); assert(toolsOffered(before).includes("load_skill")); assert(!getCurrentSystemPrompt(before).includes(body)); assertStringIncludes(getCurrentSystemPrompt(after), body); assert(!lastToolResult(after).text.includes(body)); const loadedAt = (await conversation.entries({ order: "descending" }, 1, undefined, context)).items[0].id; await writeSkill(env, "/home/.agents/skills", "release", "Changed on disk."); faux.setResponses([fauxAssistantMessage("done")]); await (await conversation.submit({ type: "input", content: "x".repeat(100_000) }, context)).wait( context, ); await conversation.waitForIdle(context); faux.setResponses([fauxAssistantMessage("A deliberately lossy summary.")]); await berry.harness.waitForTask(await conversation.compact(undefined, context), context); await berry.install(skills); const [compacted] = await turn(harness, tid, []); assertStringIncludes(getCurrentSystemPrompt(compacted), body); assert(!getCurrentSystemPrompt(compacted).includes("Changed on disk.")); const calls = faux.state.callCount; await berry.command(tid, "unskill", "release"); assertEquals(faux.state.callCount, calls); const [unloaded] = await turn(harness, tid, []); assert(!getCurrentSystemPrompt(unloaded).includes(body)); const fork = await conversation.fork(loadedAt, { ownership: { kind: "ownerless" } }, context); const [forked] = await turn(harness, await berry.tidOf(fork.id), []); assertStringIncludes(getCurrentSystemPrompt(forked), body); await berry.branch(tid, loadedAt); const [rewound] = await turn(harness, tid, []); assertStringIncludes(getCurrentSystemPrompt(rewound), body); }); Deno.test("subagents discover environment-local skills without inheriting their parent's active instructions", async () => { await using root = await tempDir(); const env = rootedEnvironment(root.path); const skills = skillsExtension(hostEnvironment(root.path)); const body = "Parent-only instructions: carefully audit the publication process."; await writeSkill(env, "/home/.agents/skills", "release", body); await using harness = await setup({ open: (name, cwd) => Promise.resolve(Object.assign(rootedEnvironment(root.path, { cwd: cwd ?? "/" }), { name })), close: () => Promise.resolve(), }); const { berry, faux, model } = harness; await berry.install(skills); const tid = await berry.create({ extensions: { environment: { cwd: "/" }, subagent: {} }, model, }); await berry.command(tid, "skill", "release"); let childPrompt: string | undefined; faux.setResponses(Array.from({ length: 4 }, () => (request) => { const last = request.messages.findLast((message) => message.role !== "system"); if (last?.role === "user" && last.content === "go") { assertStringIncludes(getCurrentSystemPrompt(request.messages), body); return fauxAssistantMessage(fauxToolCall("subagent", { task: "A separate check.", model }), { stopReason: "toolUse", }); } if (last?.role === "user") childPrompt = getCurrentSystemPrompt(request.messages); return fauxAssistantMessage("done"); })); const conversation = (await berry.conversation(tid))!; await (await conversation.submit({ type: "input", content: "go" }, context)).wait(context); await conversation.waitForIdle(context); assert(childPrompt !== undefined); assertStringIncludes(childPrompt, '"name":"release"'); assert(!childPrompt.includes(body)); });