|
| 1 | +import type { DatabaseSync, StatementSync } from 'node:sqlite'; |
| 2 | +import { bundleSchema, type AgentBundle } from '../core/contracts.js'; |
| 3 | +import { MAX_SKILL_EXPORT_FILE_BYTES, skillExportPaths } from '../core/skill-files.js'; |
| 4 | +import { Fault, canonical, noAgent, sha } from '../core/util.js'; |
| 5 | + |
| 6 | +type Statements = { prepare(sql: string): StatementSync }; |
| 7 | + |
| 8 | +/** Rebuild only changed aliases, including publications made by older gateways after rollback. */ |
| 9 | +export function installSkillExports(db: DatabaseSync): void { |
| 10 | + db.exec(` |
| 11 | + CREATE TABLE IF NOT EXISTS agent_skill_index( |
| 12 | + owner TEXT NOT NULL, agent_id TEXT NOT NULL, digest TEXT NOT NULL, |
| 13 | + PRIMARY KEY(owner,agent_id), |
| 14 | + FOREIGN KEY(owner,agent_id) REFERENCES agents(owner,id) ON DELETE CASCADE |
| 15 | + ); |
| 16 | + CREATE TABLE IF NOT EXISTS agent_skill_bytes( |
| 17 | + owner TEXT NOT NULL, agent_id TEXT NOT NULL, digest TEXT NOT NULL, sha256 TEXT NOT NULL, |
| 18 | + content BLOB NOT NULL CHECK(length(content)<=${MAX_SKILL_EXPORT_FILE_BYTES}), |
| 19 | + PRIMARY KEY(owner,agent_id,digest,sha256), |
| 20 | + FOREIGN KEY(owner,agent_id) REFERENCES agents(owner,id) ON DELETE CASCADE |
| 21 | + ); |
| 22 | + CREATE TABLE IF NOT EXISTS agent_skill_files( |
| 23 | + owner TEXT NOT NULL, agent_id TEXT NOT NULL, digest TEXT NOT NULL, skill TEXT NOT NULL, path TEXT NOT NULL, |
| 24 | + ordinal INTEGER NOT NULL, bytes INTEGER NOT NULL, sha256 TEXT NOT NULL, |
| 25 | + PRIMARY KEY(owner,agent_id,skill,path), |
| 26 | + FOREIGN KEY(owner,agent_id,digest,sha256) REFERENCES agent_skill_bytes(owner,agent_id,digest,sha256) ON DELETE CASCADE |
| 27 | + );`); |
| 28 | + const changed = db |
| 29 | + .prepare( |
| 30 | + ` |
| 31 | + SELECT a.owner,a.id,a.digest FROM agents a |
| 32 | + LEFT JOIN agent_skill_index i ON i.owner=a.owner AND i.agent_id=a.id AND i.digest=a.digest |
| 33 | + WHERE i.digest IS NULL`, |
| 34 | + ) |
| 35 | + .all(); |
| 36 | + if (!changed.length) return; |
| 37 | + const payload = db.prepare(` |
| 38 | + SELECT payload FROM bundles WHERE owner=? AND agent_id=? AND digest=? |
| 39 | + AND CASE WHEN json_valid(payload) THEN EXISTS( |
| 40 | + SELECT 1 FROM json_each(payload,'$.agent.skills') s |
| 41 | + WHERE CASE WHEN s.type='object' THEN json_type(s.value,'$.exportFiles')='array' ELSE 0 END |
| 42 | + ) ELSE 0 END`); |
| 43 | + for (const row of changed) { |
| 44 | + const owner = String(row.owner), |
| 45 | + id = String(row.id), |
| 46 | + digest = String(row.digest); |
| 47 | + const source = payload.get(owner, id, digest); |
| 48 | + const bundle = source && bundleSchema.parse(JSON.parse(String(source.payload))); |
| 49 | + if (bundle) { |
| 50 | + const { digest: storedDigest, ...data } = bundle; |
| 51 | + if (storedDigest !== digest || data.agent.id !== id || sha(canonical(data)) !== digest) |
| 52 | + throw new Fault( |
| 53 | + 'bundle_integrity', |
| 54 | + 'Skill export backfill did not match the published bundle.', |
| 55 | + ); |
| 56 | + } |
| 57 | + writeProjection(db, owner, id, digest, bundle); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** The alias and its read projection must commit or roll back together. */ |
| 62 | +export function replaceSkillExports(store: Statements, owner: string, bundle: AgentBundle): void { |
| 63 | + writeProjection(store, owner, bundle.agent.id, bundle.digest, bundle); |
| 64 | +} |
| 65 | + |
| 66 | +function writeProjection( |
| 67 | + store: Statements, |
| 68 | + owner: string, |
| 69 | + agentId: string, |
| 70 | + digest: string, |
| 71 | + bundle?: AgentBundle, |
| 72 | +): void { |
| 73 | + store.prepare('DELETE FROM agent_skill_files WHERE owner=? AND agent_id=?').run(owner, agentId); |
| 74 | + store.prepare('DELETE FROM agent_skill_bytes WHERE owner=? AND agent_id=?').run(owner, agentId); |
| 75 | + store |
| 76 | + .prepare( |
| 77 | + ` |
| 78 | + INSERT INTO agent_skill_index VALUES(?,?,?) ON CONFLICT(owner,agent_id) DO UPDATE SET digest=excluded.digest`, |
| 79 | + ) |
| 80 | + .run(owner, agentId, digest); |
| 81 | + if (!bundle?.agent.skills.some((skill) => skill.exportFiles !== undefined)) return; |
| 82 | + const insertBytes = store.prepare('INSERT INTO agent_skill_bytes VALUES(?,?,?,?,?)'); |
| 83 | + const insertFile = store.prepare('INSERT INTO agent_skill_files VALUES(?,?,?,?,?,?,?,?)'); |
| 84 | + const files = new Map(bundle.files.map((file) => [file.path, file])); |
| 85 | + const saved = new Set<string>(); |
| 86 | + for (const skill of bundle.agent.skills) { |
| 87 | + let ordinal = 0; |
| 88 | + for (const [path, bundlePath] of skillExportPaths(skill)) { |
| 89 | + const file = files.get(bundlePath); |
| 90 | + if (!file) throw new Fault('missing_skill', 'A declared skill export file is missing.'); |
| 91 | + if (!saved.has(file.sha256)) { |
| 92 | + insertBytes.run(owner, agentId, digest, file.sha256, Buffer.from(file.content, 'base64')); |
| 93 | + saved.add(file.sha256); |
| 94 | + } |
| 95 | + insertFile.run( |
| 96 | + owner, |
| 97 | + agentId, |
| 98 | + digest, |
| 99 | + skill.name, |
| 100 | + path, |
| 101 | + ordinal++, |
| 102 | + Buffer.byteLength(file.content, 'base64'), |
| 103 | + file.sha256, |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +type SkillRead = { owner: string; agent: string; skill: string; agent_version?: string }; |
| 110 | +const filesForAlias = ` |
| 111 | + FROM agents a LEFT JOIN agent_skill_files s |
| 112 | + ON s.owner=a.owner AND s.agent_id=a.id AND s.digest=a.digest AND s.skill=?`; |
| 113 | +const activeAlias = 'WHERE a.owner=? AND a.id=? AND a.digest=coalesce(?,a.digest)'; |
| 114 | +const missingExport = () => |
| 115 | + new Fault('skill_export_not_found', 'The skill file is not exported by this publication.', 404); |
| 116 | + |
| 117 | +export function readSkillManifest(store: Statements, query: SkillRead) { |
| 118 | + const rows = store |
| 119 | + .prepare( |
| 120 | + ` |
| 121 | + SELECT a.digest,s.path,s.bytes,s.sha256 |
| 122 | + ${filesForAlias} ${activeAlias} ORDER BY s.ordinal`, |
| 123 | + ) |
| 124 | + .all(query.skill, query.owner, query.agent, query.agent_version ?? null); |
| 125 | + if (!rows.length) throw noAgent(); |
| 126 | + if (rows[0]!.path === null) throw missingExport(); |
| 127 | + return { |
| 128 | + agent: query.agent, |
| 129 | + agent_version: String(rows[0]!.digest), |
| 130 | + skill: query.skill, |
| 131 | + files: rows.map((row) => ({ |
| 132 | + path: String(row.path), |
| 133 | + bytes: Number(row.bytes), |
| 134 | + sha256: String(row.sha256), |
| 135 | + })), |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +export function readSkillFile( |
| 140 | + store: Statements, |
| 141 | + query: SkillRead & { agent_version: string; file: string }, |
| 142 | +): Buffer { |
| 143 | + const row = store |
| 144 | + .prepare( |
| 145 | + ` |
| 146 | + SELECT s.path,s.bytes,s.sha256,b.content |
| 147 | + ${filesForAlias} AND s.path=? |
| 148 | + LEFT JOIN agent_skill_bytes b |
| 149 | + ON b.owner=s.owner AND b.agent_id=s.agent_id AND b.digest=s.digest AND b.sha256=s.sha256 |
| 150 | + ${activeAlias}`, |
| 151 | + ) |
| 152 | + .get(query.skill, query.file, query.owner, query.agent, query.agent_version); |
| 153 | + if (!row) throw noAgent(); |
| 154 | + if (row.path === null) throw missingExport(); |
| 155 | + const content = row.content; |
| 156 | + if ( |
| 157 | + !(content instanceof Uint8Array) || |
| 158 | + content.byteLength > MAX_SKILL_EXPORT_FILE_BYTES || |
| 159 | + content.byteLength !== row.bytes |
| 160 | + ) |
| 161 | + throw new Error('Stored skill export failed its integrity check.'); |
| 162 | + const bytes = Buffer.from(content.buffer, content.byteOffset, content.byteLength); |
| 163 | + if (sha(bytes) !== row.sha256) throw new Error('Stored skill export failed its integrity check.'); |
| 164 | + return bytes; |
| 165 | +} |
0 commit comments