Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305import assert from "node:assert/strict";import { test } from "node:test";import { setTimeout as sleep } from "node:timers/promises";import { build } from "esbuild";import { Miniflare, convertV4MiniflareOptions } from "miniflare";
const envelope = (name, version = "1.0.0") => ({ formatVersion: 1, files: [ { path: "SKILL.md", content: `---\nname: ${name}\ndescription: Metadata for ${name}\nmetadata:\n version: "${version}"\n---\nPRIVATE INSTRUCTIONS ${name} VERSION ${version}.`, }, ],});const selection = (skill, enabled) => ({ source: skill.origin.kind === "bundled" ? "bundled" : "installed", name: skill.name, fingerprint: skill.fingerprint, revision: skill.revision, enabled,});const toolOutput = (snapshot) => snapshot.messages .flatMap((message) => message.parts) .filter( (part) => part.type === "tool-activate_skill" || part.toolName === "activate_skill", ) .map((part) => part.output) .filter((output) => output !== undefined);async function until(read, predicate, label, signal) { const deadline = Date.now() + 10_000; do { signal.throwIfAborted(); const value = await read(); if (predicate(value)) return value; await sleep(10, undefined, { signal }); } while (Date.now() < deadline); assert.fail(`Timed out: ${label}`);}
test( "native conversation Skills activate lazily with current authority and durable activity", { timeout: 90_000 }, async (t) => { const bundle = await build({ entryPoints: ["tests/fixtures/skill-activation-worker.ts"], alias: { path: "node:path", crypto: "node:crypto", async_hooks: "node:async_hooks", }, target: "es2022", keepNames: true, bundle: true, write: false, format: "esm", platform: "neutral", conditions: ["workerd", "worker", "browser"], mainFields: ["module", "main"], external: ["cloudflare:*", "node:*"], }); const worker = new Miniflare( convertV4MiniflareOptions({ modules: true, script: bundle.outputFiles[0].text, compatibilityDate: "2026-09-04", compatibilityFlags: ["nodejs_compat"], durableObjects: { MODEL: { className: "PersonalAgent", useSQLite: true }, }, r2Buckets: ["ATTACHMENTS"], }), ); const requests = new AbortController(); const signal = AbortSignal.any([t.signal, requests.signal]); let disposal; const cleanup = () => { requests.abort(); return (disposal ??= (async () => { let timer; try { await Promise.race([ worker.dispose(), new Promise((_, reject) => { timer = setTimeout( () => reject(new Error("Skill fixture disposal timed out")), 15_000, ); }), ]); } finally { clearTimeout(timer); } })()); }; t.after(cleanup, { timeout: 16_000 }); const poll = (read, predicate, label) => until(read, predicate, label, signal); const pending = new Map(); const call = async (action, value, name = "conversation") => { const response = await worker.dispatchFetch("https://fixture.example/", { method: "POST", signal, body: JSON.stringify({ action, value, name }), }); assert.equal(response.status, 200); const result = await response.json(); assert.ok(!result?.fixtureError, JSON.stringify(result)); return result; }; const list = () => call("list"); const skill = async (name) => (await list()).find((item) => item.name === name); const toggle = async (item, enabled) => { const result = await call("toggle", selection(item, enabled)); assert.equal(result.ok, true, JSON.stringify(result)); return skill(item.name); }; const activate = (name, names, hold = false) => call("submit", { names, hold }, name); const start = (name, names, hold = false) => { const work = activate(name, names, hold); pending.set(name, work); return work; }; const finish = async (name) => { const result = await pending.get(name); pending.delete(name); return result; }; const assertFailed = (snapshot, original) => { assert.deepEqual(toolOutput(snapshot), [ `Skill not found: ${original.name}`, ]); const activity = snapshot.activities.find( (item) => item.toolName === "activate_skill", ); assert.equal(activity.status, "failed"); assert.equal(activity.capability.fingerprint, original.fingerprint); assert.deepEqual(activity.capability.source, { kind: "skill", id: `installed:${original.name}`, name: original.name, }); assert.ok( !JSON.stringify(snapshot.messages).includes("PRIVATE INSTRUCTIONS"), ); }; try { for (const item of await list()) await toggle(item, false); const alpha = await call("install", { envelope: envelope("alpha-guide"), }); const beta = await call("install", { envelope: envelope("beta-guide") }); const beforeCatalog = (await call("metrics")).reads; const empty = await activate("same-instance", []); assert.ok( empty.observed.every((step) => !step.tools.includes("activate_skill")), ); assert.equal((await call("metrics")).reads, beforeCatalog); await toggle(alpha, true); await toggle(beta, true); const metadata = await activate("same-instance", []); assert.equal(metadata.instanceId, empty.instanceId); assert.ok(metadata.observed[0].tools.includes("activate_skill")); assert.ok(metadata.observed[0].tools.includes("read_skill_resource")); assert.match(metadata.observed[0].prompt, /Metadata for alpha-guide/); assert.equal( metadata.observed[0].prompt.split("CURRENT OWNER INSTRUCTIONS:") .length - 1, 1, ); assert.ok(!metadata.observed[0].prompt.includes("PRIVATE INSTRUCTIONS")); assert.equal( (await call("metrics")).reads, beforeCatalog, "catalog refresh and ordinary turns do not read native R2", );
start("same-instance", ["alpha-guide", "beta-guide"], true); const raw = await poll( () => call("snapshot", undefined, "same-instance"), (value) => value.holding && value.activities.some((item) => item.toolName === "activate_skill"), "raw activation start before arguments", ); const rawActivity = raw.activities.find( (item) => item.toolName === "activate_skill", ); assert.equal(rawActivity.capability, undefined); await call("release", undefined, "same-instance"); const activated = await finish("same-instance"); assert.equal(activated.instanceId, empty.instanceId); assert.equal( toolOutput(activated).length, 2, JSON.stringify(activated.messages), ); for (const name of ["alpha-guide", "beta-guide"]) { assert.match( activated.observed.at(-1).prompt, new RegExp(`PRIVATE INSTRUCTIONS ${name}`), ); const current = await skill(name); const activity = activated.activities.find( (item) => item.capability?.name === name, ); assert.equal(activity.status, "succeeded"); assert.equal(activity.capability.fingerprint, current.fingerprint); assert.deepEqual(activity.capability.source, { kind: "skill", id: `installed:${name}`, name, }); } assert.equal( activated.activities.find( (item) => item.toolCallId === rawActivity.toolCallId, ).capability.name, "alpha-guide", ); assert.equal((await call("metrics")).reads, beforeCatalog + 2);
const original = await skill("alpha-guide"); const beforeDisable = (await call("metrics")).reads; start("disabled-before-input", [original.name], true); await poll( () => call("snapshot", undefined, "disabled-before-input"), (value) => value.holding, "paused model input", ); await toggle(original, false); await call("release", undefined, "disabled-before-input"); assertFailed(await finish("disabled-before-input"), original); assert.equal( (await call("metrics")).reads, beforeDisable, "disabled activation cannot reach R2", );
for (const race of ["disable", "replace"]) { const current = await toggle(await skill("alpha-guide"), true); await call("fault", { holdRead: true }); const name = `${race}-during-load`; start(name, [current.name]); await poll( () => call("metrics"), (value) => value.waitingRead, "real R2 load held across authority change", ); if (race === "disable") await toggle(current, false); else await call("install", { envelope: envelope(current.name, "2.0.0"), revision: current.revision, }); await call("fault", { holdRead: false }); assertFailed(await finish(name), current); }
await toggle(await skill("alpha-guide"), true); await call("fault", { failRead: true }); const failedRead = await activate("failed-read", ["alpha-guide"]); assertFailed(failedRead, await skill("alpha-guide")); assert.ok(!JSON.stringify(failedRead).includes("Fixture private")); await call("fault", { failRead: false }); const recoveredRead = await activate("failed-read", ["alpha-guide"]); assert.match( toolOutput(recoveredRead).at(-1), /PRIVATE INSTRUCTIONS alpha-guide VERSION 2.0.0/, ); await call("fault", { failCatalog: true }); const catalogFailure = await activate("same-instance", []); assert.equal(catalogFailure.instanceId, empty.instanceId); assert.ok( catalogFailure.observed.every( (step) => !step.tools.includes("activate_skill"), ), ); assert.ok( !JSON.stringify(catalogFailure).includes("Fixture private catalog"), ); await call("fault", { failCatalog: false }); const catalogRecovered = await activate("same-instance", []); assert.equal(catalogRecovered.instanceId, empty.instanceId); assert.ok(catalogRecovered.observed[0].tools.includes("activate_skill")); } finally { if (!t.signal.aborted) await Promise.allSettled([ call("fault", { holdRead: false }), ...[...pending.keys()].map((name) => call("release", undefined, name), ), ]); const settled = Promise.allSettled(pending.values()); await cleanup(); await settled; } },);