diff --git a/docs/global-skills-plan.md b/docs/global-skills-plan.md new file mode 100644 index 00000000..cff7f7c3 --- /dev/null +++ b/docs/global-skills-plan.md @@ -0,0 +1,217 @@ +# Global skills: implementation plan + +Status: proposed implementation plan; no application behavior or existing jobs changed. + +## Product contract + +A user defines skills globally within their Factorize account. Every project in +that account can use them. In storage and authorization, use the existing tenant +boundary; global never means shared between accounts. + +A skill is a name, immutable key, and literal text body. Jobs explicitly include +skills in their existing prompt template: + +```text +{{skills.exe_dev}} +{{skills.factorize_project}} +{{skills.development}} + +Complete this task: +{{trigger-1.prompt}} +``` + +Insert each body exactly where referenced, preserving whitespace and order. +Repeated references insert repeated text. Bodies are never parsed or rendered: +`{{trigger-1.prompt}}` and `{{skills.other}}` inside a body remain literal text. +There is no nesting, parameterization, automatic inclusion, project inheritance, +remote fetching, or agent-side skill discovery. Inclusion does not grant access +to repositories or integrations. + +Support skills in job prompts only in v1, not run names or invocation task text. +Retain existing Mustache behavior for ordinary job-template values and sections. +Reserve the `skills` namespace and resolve it from server-owned data, independent +of trigger data and the current Mustache section context. + +Allow ordinary escaped/unescaped interpolation forms for `skills.KEY`; all insert +the same plain text, as existing prompt interpolation does. Reject skill sections, +inverted sections, partials, bare `skills`, and deeper paths in job templates. +Skill interpolation inside an ordinary trigger section is allowed. Validate all +references at save time, even those inside a currently inactive section. Parse +the job template using the actual Mustache parser, not a regex. Text in comments +and literal text are not references. Skill-looking text inside a skill body is +never scanned for dependencies. + +## User experience + +- Add Settings > Skills, available independently of the selected project. +- List skill name, key, revision, last update, and number of jobs using it. + Provide search and create/edit/delete actions with loading and error states. +- Creation suggests a key from the name; the user can edit it before saving. + Names can change later; keys cannot. Show the exact inclusion syntax. +- Edit the body in a plain-text textarea. Explain that template syntax in the + body stays literal. Preserve leading/trailing whitespace on save. +- Show linked jobs grouped by project, including disabled jobs. Before saving an + edit, show that future runs of those jobs will use the new content. Do not add + a separate approval workflow. +- Block deletion while any saved job references the skill. Return the referencing + jobs so users can remove references first. Historical runs do not block deletion. +- Add an Insert skill picker to the job prompt editor, inserting at the cursor. + Do not add this picker to run names. Show directly referenced skills and errors. +- Provide a server-backed prompt preview for unsaved drafts. Support an explicit + sample trigger context and clearly identify it as sample data. Without sample + data, show a skill-expanded source view that preserves unresolved trigger tags; + do not mislabel it as the final runtime prompt. Never render skill text as HTML. +- Run details show the final prompt and the skill names, keys, revisions, and + exact bodies captured for that run. Distinguish historical snapshots from links + to the current skill. Legacy runs show no recorded skill provenance. + +## Data and concurrency + +Add tenant-scoped `skills` records with id, immutable key, name, encrypted body, +integer revision, createdAt, and updatedAt. Require a unique (tenantId, key). +Use the existing encryption approach for prompts. Recommended initial limits: +name 120 characters; key 64 characters matching `[a-z][a-z0-9_]*`; body 50,000 +characters with at least one non-whitespace character. Reject prototype-related +reserved keys and use own-property lookups rather than inherited object fields. + +Require expectedRevision for new skill update/delete APIs. An edit increments +revision; stale writes return 409. Updating a name also creates a new revision so +snapshot metadata has a single coherent revision identifier. No version pinning +or revision-history UI in v1. + +Add a `job_skill_references` relation with tenant-qualified job/skill foreign keys. +Derive it from the prompt source, never accept it as caller-maintained truth. +Save the job and its reference set transactionally. Foreign-key restrictions and +appropriate locking must prevent a concurrent delete from creating a dangling +reference. Disabled jobs count; unsaved editor drafts do not. Moving jobs between +projects does not affect skill resolution. + +Add a nullable encrypted skill snapshot to job runs, storing each referenced +skill's id, key, name, revision, and exact body. The final encrypted prompt remains +the authoritative agent input. Snapshot all direct dependencies, including those +in inactive sections, and identify actually inserted skills separately in preview +and provenance if useful. Store snapshots independently of live skill rows so +deletion does not destroy history. Follow existing run retention/deletion rules. + +Resolve referenced skills in one consistent database read and persist the prompt +and snapshot together. A concurrent edit may fall before or after that read, but +the recorded revision, body, and rendered prompt must agree. Batch lookups; avoid +one database query per occurrence. + +## Rendering and execution + +Build one skill-reference parser/resolver used by job validation, preview, and +InvocationService. Extend the existing plain-text Mustache writer to resolve +skill interpolation from a separate trusted map, including inside sections. +Do not expand bodies into template source and then run Mustache over that source. +Do not scan rendered output: trigger data that contains a skill reference must +remain ordinary data. Do not let input data override server-resolved skills. + +Resolve current skill revisions when an invocation creates its queued run. +Queued/running runs keep their snapshot after edits. Duplicate idempotency keys +return the original invocation/run without resolving newer revisions. Backend +launch retries reuse the stored prompt; a genuinely new invocation uses current +skills. Skill edits alone do not create runs or emit job-edited lifecycle events. + +Wire the common path into manual, scheduled, webhook, and lifecycle invocations, +including scheduler recovery paths. Existing run launch code should continue to +consume the persisted prompt without loading skills again. + +Missing references fail job save, preview, and invocation with an actionable +error; never substitute an empty string. Failed automatic invocations must record +the configuration error in existing delivery/scheduler diagnostics without +launching a VM or endlessly retrying a permanent configuration error. + +Enforce a bounded rendered prompt during construction, counting repeats and +Mustache section expansion. Proposed skill-enabled prompt limit: 200,000 UTF-8 +bytes; confirm backend transport limits before implementation. Preserve existing +limits and behavior for jobs without skills in this change. Preview reports the +actual size. A skill edit can make a future composed prompt too large, so both +preview and invocation must enforce the limit, not just job save. + +## REST, MCP, and authorization + +Expose documented operations under `/api/v1`, served through protected-api.ts +and the existing contract/service machinery: + +| Operation | Route | Required scopes | +| --- | --- | --- | +| List skills, paginated metadata | GET /api/v1/skills | skills:read | +| Create skill | POST /api/v1/skills | skills:write | +| Read skill | GET /api/v1/skills/{skillId} | skills:read | +| Replace editable fields | PUT /api/v1/skills/{skillId} | skills:write | +| Delete unused skill | DELETE /api/v1/skills/{skillId} | skills:write | +| List referencing jobs | GET /api/v1/skills/{skillId}/jobs | skills:read + jobs:read | +| Preview unsaved prompt | POST /api/v1/jobs/prompt-preview | jobs:read + skills:read | + +Preview accepts promptTemplate, optional sample context, and source/final mode; +returns text, references/revisions, byte count, and validation diagnostics. +It neither saves a job nor invokes one. Skill mutations return metadata rather +than implicitly granting read access to a write-only caller. + +Add skills:read and skills:write to OAuth consent, API-key configuration, session +scopes, scope normalization, discovery, documentation, and tests. Do not silently +expand existing delegated grants. Require skills:read in addition to jobs:write +when creating/changing a prompt that includes skills. Existing clients may edit +unrelated job fields while preserving an unchanged prompt without acquiring a +new scope. Existing runs:invoke permissions can execute saved jobs; execution +resolves dependencies internally, including automation without a user token. + +Runs:read continues to expose the complete executed prompt, including included +skill text, just as it exposes instructions today. Explain that skills are reusable +instructions, not secret storage; skills:read controls library access, not removal +of text from authorized run history. Usage metadata must not disclose job names +to callers without jobs:read. + +Provide MCP list_skills, get_skill, create_skill, update_skill, delete_skill, +list_skill_jobs, and preview_job_prompt through the same service layer and scope +rules. Keep existing job writes and conditional-update omission compatibility. +Update packages/docs/openapi.yaml and regenerate factorize-api-client; the frontend +must use that client and never import backend rendering code. + +## Implementation sequence and validation + +1. Add schema migration, encrypted storage, revision checks, usage relation, and + tenant isolation. Test conflicting keys, stale writes, job-save/delete races, + and preserving snapshots after deletion. +2. Implement reference parsing, literal insertion, validation, size accounting, + and the shared invocation resolver. Cover every trigger path and idempotency. +3. Add REST/MCP contracts, scopes, preview, run provenance, and generated client. + Test authorization at boundaries and cross-account reference attempts. +4. Build Skills settings, job insertion/preview, usage links, and run provenance. + Browser tests cover create/include/edit/preview/delete-blocked flows, stale + edits, missing references, and literal braces in skill bodies. +5. Document syntax, permissions, limits, update timing, and migration examples. + Run npm run generate after contract changes. With Node 24.12.0 and npm 11.19.0, + run npm ci for a clean checkout, bootstrap isolated test prerequisites as + needed, and run the root npm test after the final code change. Include boundary + validation; targeted iteration tests do not replace the root suite. + +Critical rendering cases: whitespace/Unicode, HTML-like text, literal Mustache +in skill bodies and trigger payloads, repeated references, inactive/looped +sections, alternate delimiters, malformed and unknown references, reserved keys, +section-local shadowing, cross-account isolation, and expanded-size overflow. +Regression tests must show unchanged behavior for jobs with no skill references. + +## Release and migration + +Use additive schema and API changes. Deploy the skill-aware backend before the +frontend can save references. Check existing prompt sources for use of the newly +reserved skills namespace before enabling the feature. Do not alter existing +jobs automatically; an empty skills library leaves all current jobs unchanged. + +Once skill-backed prompts are saved, rolling back to a backend that silently +ignores skills is unsafe. Retain the compatible renderer during rollback, or +first disable/migrate affected jobs to literal prompts. Disabling only the UI +does not make an old renderer safe. Preserve existing deployment/asset gates. + +Initial manual migration candidates: exe_dev, factorize_project, anarack_project, +then shared development/dispatcher/merge procedures. Compare effective prompts +before and after extraction. Preserve project-specific checkpoint, validation, +recovery, and authorization differences; do not mechanically merge divergent +workflows. Skill updates are not deployments of permission changes. + +Out of scope: nested or parameterized skills, implicit inheritance, external skill +imports, AGENTS.md generation, project resource pickers, workflow engines, version +pinning, and changing runtime task/run-identity contracts. Those can be separate +features after this explicit composition model is established. diff --git a/packages/api/src/postgres/skill-repository.ts b/packages/api/src/postgres/skill-repository.ts index 1621395e..991ecbf0 100644 --- a/packages/api/src/postgres/skill-repository.ts +++ b/packages/api/src/postgres/skill-repository.ts @@ -7,7 +7,16 @@ export class SkillRepository { constructor(private db: Database, private tenantId: string, private encryptionKey: string) {} private async map(row: SkillRow, usageCount = 0, body = false) { return { id: row.id, key: row.key, revision: row.revision, ...(body ? { body: await decrypt(row.encrypted_body, this.encryptionKey) } : {}), usageCount, createdAt: row.created_at.toISOString(), updatedAt: row.updated_at.toISOString() }; } async list(q = "", limit = 30, cursor?: string) { const result = await this.db.pool.query(`SELECT s.*, count(js.job_id)::text usage_count FROM app.skills s LEFT JOIN app.job_skills js ON js.tenant_id=s.tenant_id AND js.skill_id=s.id WHERE s.tenant_id=$1 AND ($2='' OR s.key ILIKE '%'||$2||'%') AND ($3::uuid IS NULL OR s.id < $3::uuid) GROUP BY s.tenant_id, s.id ORDER BY s.id DESC LIMIT $4`, [this.tenantId, q, cursor ?? null, limit + 1]); const page = result.rows.slice(0, limit); return { items: await Promise.all(page.map(row => this.map(row, Number(row.usage_count)))), nextCursor: result.rows.length > limit ? page.at(-1)!.id : null }; } - async get(idOrKey: string, includeBody = true) { const row = (await this.db.pool.query("SELECT * FROM app.skills WHERE tenant_id=$1 AND (id=$2::uuid OR key=$2::text) LIMIT 1", [this.tenantId, idOrKey])).rows[0]; return row ? this.map(row, 0, includeBody) : null; } + async get(idOrKey: string, includeBody = true) { + // A shared parameter for UUID and text comparisons is inferred as UUID by + // PostgreSQL. Keep the key parameter separate, and never cast keys to UUID. + const id = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(idOrKey) ? idOrKey : null; + const row = (await this.db.pool.query( + "SELECT * FROM app.skills WHERE tenant_id=$1 AND (id=$2::uuid OR key=$3::text) LIMIT 1", + [this.tenantId, id, idOrKey], + )).rows[0]; + return row ? this.map(row, 0, includeBody) : null; + } async create(key: string, body: string) { try { const row = (await this.db.pool.query("INSERT INTO app.skills(tenant_id,id,key,encrypted_body) VALUES($1,$2,$3,$4) RETURNING *", [this.tenantId, crypto.randomUUID(), key, await encrypt(body, this.encryptionKey)])).rows[0]; return this.map(row, 0, true); } catch (e: any) { if (e.code === "23505") throw new SkillConflict("Skill key is already in use."); throw e; } } async update(id: string, input: { key?: string; body?: string; expectedRevision: number }) { const row = (await this.db.pool.query("SELECT * FROM app.skills WHERE tenant_id=$1 AND id=$2 FOR UPDATE", [this.tenantId, id])).rows[0]; if (!row) return null; if (row.revision !== input.expectedRevision) throw new SkillConflict("Skill changed since it was loaded."); try { const updated = (await this.db.pool.query("UPDATE app.skills SET key=COALESCE($3,key),encrypted_body=COALESCE($4,encrypted_body),revision=revision+1,updated_at=now() WHERE tenant_id=$1 AND id=$2 RETURNING *", [this.tenantId, id, input.key ?? null, input.body === undefined ? null : await encrypt(input.body, this.encryptionKey)])).rows[0]; return this.map(updated, 0, true); } catch (e: any) { if (e.code === "23505") throw new SkillConflict("Skill key is already in use."); throw e; } } async remove(id: string, expectedRevision: number) { return this.db.transaction(async client => { const skill = (await client.query("SELECT * FROM app.skills WHERE tenant_id=$1 AND id=$2 FOR UPDATE", [this.tenantId, id])).rows[0]; if (!skill) return false; if (skill.revision !== expectedRevision) throw new SkillConflict("Skill changed since it was loaded."); const refs = (await client.query("SELECT j.id,j.name,j.slug,j.enabled,j.project_id FROM app.jobs j JOIN app.job_skills js ON js.tenant_id=j.tenant_id AND js.job_id=j.id WHERE js.tenant_id=$1 AND js.skill_id=$2 ORDER BY j.name", [this.tenantId, id])).rows; if (refs.length) throw new SkillReferenced(refs); await client.query("DELETE FROM app.skills WHERE tenant_id=$1 AND id=$2", [this.tenantId, id]); return true; }); } diff --git a/packages/api/test/protocol-api.postgres.test.ts b/packages/api/test/protocol-api.postgres.test.ts index bbd0bc90..0793d115 100644 --- a/packages/api/test/protocol-api.postgres.test.ts +++ b/packages/api/test/protocol-api.postgres.test.ts @@ -1,3 +1,4 @@ +import { SkillRepository } from "../src/postgres/skill-repository"; import worker from "../src/worker"; import { describe, expect, it, vi } from "vitest"; import { loadingFixture } from "./helpers/loading-fixture"; @@ -6,7 +7,8 @@ import { deviceAuthorization } from "../src/device-oauth"; vi.mock("cloudflare:workers", () => ({ WorkerEntrypoint: class {}, DurableObject: class {} })); vi.mock("@cloudflare/workers-oauth-provider", () => ({ OAuthProvider: class {}, AuthorizationError: class extends Error {}, getOAuthApi: vi.fn() })); const url = process.env.AUTH_TEST_DATABASE_URL; -describe.skipIf(!url)("cookie protocol APIs against PostgreSQL", () => { +// Each test creates and migrates an isolated database before exercising the API. +describe.skipIf(!url)("cookie protocol APIs against PostgreSQL", { timeout: 30_000 }, () => { it("lists skills and their usage through an existing browser session", async () => { const f = await loadingFixture(url!); try { @@ -35,6 +37,41 @@ describe.skipIf(!url)("cookie protocol APIs against PostgreSQL", () => { } finally { await f.cleanup(); } }); + it("reads, previews and attaches persisted skills through the API", async () => { + const f = await loadingFixture(url!); + try { + const request = (path: string, method = "GET", body?: unknown) => worker.fetch(new Request(f.env.APP_ORIGIN + "/api/v1" + path, { + method, headers: { Cookie: f.cookie, Origin: f.env.APP_ORIGIN, "Content-Type": "application/json" }, + ...(body === undefined ? {} : { body: JSON.stringify(body) }), + }), f.env, {} as ExecutionContext); + const body = "Literal skill {{notATemplate}}"; + const created = await request("/skills", "POST", { key: "integration-context", body }); + expect(created.status).toBe(201); + const skill = await created.json() as { id: string }; + const read = await request(`/skills/${skill.id}`); + expect(read.status).toBe(200); + expect(await read.json()).toMatchObject({ id: skill.id, body }); + const repository = new SkillRepository(f.db, f.tenantId, f.env.CREDENTIAL_ENCRYPTION_KEY); + expect(await repository.get("integration-context")).toMatchObject({ id: skill.id, body }); + expect(await repository.get("missing-key")).toBeNull(); + expect(await repository.get(crypto.randomUUID())).toBeNull(); + const other = new SkillRepository(f.db, f.otherTenant, f.env.CREDENTIAL_ENCRYPTION_KEY); + expect(await other.get(skill.id)).toBeNull(); + expect(await other.get("integration-context")).toBeNull(); + const preview = await request("/skills/preview", "POST", { skillIds: [skill.id], jobContext: "Job task" }); + expect(preview.status).toBe(200); + expect(await preview.json()).toMatchObject({ prompt: body + "\n\nJob task" }); + const saved = await request(`/jobs/${f.jobIds[0]}`, "PUT", { + name: "Skill job", slug: "skill-job", promptTemplate: "Job task", executionConfigId: "local", + concurrencyLimit: 2, triggers: [], skillIds: [skill.id], + }); + expect(saved.status).toBe(200); + const job = await f.request(`/api/v1/jobs/${f.jobIds[0]}`); + expect(job.status).toBe(200); + expect(await job.json()).toMatchObject({ promptTemplate: "Job task", skills: [{ id: skill.id, key: "integration-context", revision: 1, position: 0 }] }); + } finally { await f.cleanup(); } + }); + it("checks real owner/session version before consent and rejects revoked sessions", async () => { const f = await loadingFixture(url!); try {