@mago-foundation/mago-ski 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +30 -3
  2. package/dist/hosts/github/run.d.ts +3 -1
  3. package/dist/hosts/github/run.js +29 -9
  4. package/dist/hosts/pi/index.js +48 -5
  5. package/dist/src/admin.d.ts +136 -1
  6. package/dist/src/admin.js +221 -84
  7. package/dist/src/cli.js +335 -10
  8. package/dist/src/fs-safe.d.ts +15 -2
  9. package/dist/src/fs-safe.js +32 -3
  10. package/dist/src/gate/adapters/claude-code-hook.d.ts +1 -0
  11. package/dist/src/gate/adapters/claude-code-hook.js +4 -0
  12. package/dist/src/gate/adapters/claude-code-settings.d.ts +13 -0
  13. package/dist/src/gate/adapters/claude-code-settings.js +49 -0
  14. package/dist/src/gate/adapters/claude-code.d.ts +33 -0
  15. package/dist/src/gate/adapters/claude-code.js +193 -0
  16. package/dist/src/gate/adapters/client.d.ts +13 -0
  17. package/dist/src/gate/adapters/client.js +57 -0
  18. package/dist/src/gate/adapters/codex-hook.d.ts +1 -0
  19. package/dist/src/gate/adapters/codex-hook.js +5 -0
  20. package/dist/src/gate/adapters/codex-requirements.d.ts +19 -0
  21. package/dist/src/gate/adapters/codex-requirements.js +56 -0
  22. package/dist/src/gate/adapters/codex.d.ts +16 -0
  23. package/dist/src/gate/adapters/codex.js +574 -0
  24. package/dist/src/gate/adapters/hook-main.d.ts +7 -0
  25. package/dist/src/gate/adapters/hook-main.js +35 -0
  26. package/dist/src/gate/adapters/host-pid.d.ts +1 -0
  27. package/dist/src/gate/adapters/host-pid.js +20 -0
  28. package/dist/src/gate/admission.d.ts +54 -0
  29. package/dist/src/gate/admission.js +490 -0
  30. package/dist/src/gate/contract.d.ts +51 -0
  31. package/dist/src/gate/contract.js +94 -0
  32. package/dist/src/gate/decision-log.d.ts +38 -0
  33. package/dist/src/gate/decision-log.js +251 -0
  34. package/dist/src/gate/generations.d.ts +67 -0
  35. package/dist/src/gate/generations.js +518 -0
  36. package/dist/src/gate/proc.d.ts +31 -0
  37. package/dist/src/gate/proc.js +79 -0
  38. package/dist/src/gate/refresh.d.ts +11 -0
  39. package/dist/src/gate/refresh.js +30 -0
  40. package/dist/src/gate/run-verifier.d.ts +23 -0
  41. package/dist/src/gate/run-verifier.js +89 -0
  42. package/dist/src/gate/service.d.ts +80 -0
  43. package/dist/src/gate/service.js +722 -0
  44. package/dist/src/gate/strict-json.d.ts +11 -0
  45. package/dist/src/gate/strict-json.js +190 -0
  46. package/dist/src/gate/trust-store.d.ts +36 -0
  47. package/dist/src/gate/trust-store.js +251 -0
  48. package/dist/src/gate/verifier-worker.d.ts +8 -0
  49. package/dist/src/gate/verifier-worker.js +66 -0
  50. package/dist/src/index.d.ts +2 -0
  51. package/dist/src/index.js +2 -0
  52. package/dist/src/keys.d.ts +4 -0
  53. package/dist/src/keys.js +23 -14
  54. package/dist/src/policy.d.ts +145 -0
  55. package/dist/src/policy.js +554 -35
  56. package/dist/src/recovery-admin.d.ts +173 -0
  57. package/dist/src/recovery-admin.js +400 -0
  58. package/dist/src/root-events.d.ts +107 -0
  59. package/dist/src/root-events.js +376 -0
  60. package/dist/src/timestamp.d.ts +61 -0
  61. package/dist/src/timestamp.js +107 -0
  62. package/dist/src/tree-digest.d.ts +1 -0
  63. package/dist/src/tree-digest.js +1 -1
  64. package/dist/src/verify.d.ts +33 -0
  65. package/dist/src/verify.js +4 -2
  66. package/docs/hosts/adapter-interface.md +8 -2
  67. package/docs/hosts/github-action.md +1 -1
  68. package/docs/hosts/pi.md +2 -0
  69. package/docs/spec/gui-admin-rack-v1.md +289 -0
  70. package/docs/spec/root-recovery-v1.md +33 -12
  71. package/docs/spec/trust-documents-v1.md +8 -2
  72. package/docs/spec/verification.md +9 -2
  73. package/docs/threat-model.md +4 -2
  74. package/package.json +3 -1
package/dist/src/admin.js CHANGED
@@ -5,9 +5,10 @@ import { issueCertificate, parseCertificate } from "./certificate.js";
5
5
  import { readRegularFile, realpathAllowingMissing, resolveOutside, writeFileAtomic, writeNewFile } from "./fs-safe.js";
6
6
  import { loadPrivateKey, loadPublicKey, validateKeyId } from "./keys.js";
7
7
  import { validateDigest, validateScope } from "./names.js";
8
+ import { isDisplaySafeText } from "./root-events.js";
8
9
  import { readRevocationsForUpdate, signRevocations } from "./revocations.js";
9
10
  import { addDuration, truncateToSecond } from "./time.js";
10
- import { CERTIFICATE_FILENAME, collectSkillTree, requireSkillRoot, SKILL_FILENAME } from "./tree-digest.js";
11
+ import { CERTIFICATE_FILENAME, collectSkillTree, sha256Hex, requireSkillRoot, SKILL_FILENAME } from "./tree-digest.js";
11
12
  import { MAX_APPROVERS, MAX_SCOPES, readTrustRootForUpdate, signTrustRoot } from "./trust-root.js";
12
13
  import { skillNameFromSkillFile } from "./verify.js";
13
14
  export const DEFAULT_TRUST_ROOT_EXPIRY = '365d';
@@ -78,91 +79,177 @@ export async function initRoot(options) {
78
79
  throw error;
79
80
  }
80
81
  }
81
- async function updateTrustRoot(options, change) {
82
- const root = await loadPrivateKey(options.rootKeyPath);
83
- const rawBytes = await readRegularFile(options.trustRootPath, 'Trust root', MAX_TRUST_ROOT_BYTES);
84
- const current = readTrustRootForUpdate(rawBytes, root.keyId);
85
- const issuedAt = truncateToSecond(options.now);
86
- const expiresAt = options.expires
87
- ? addDuration(issuedAt, options.expires, 'trust root expiry')
82
+ /** A document was older than the lowest version this computer's memory allows. The GUI maps it by class. */
83
+ export class StaleDocumentError extends Error {
84
+ constructor(label, version, minimum) {
85
+ super(`${label} version ${version} is older than version ${minimum} this computer has seen`);
86
+ this.name = 'StaleDocumentError';
87
+ }
88
+ }
89
+ /** A document changed between prepare and commit. The GUI maps it, and `FileChangedError`, by class. */
90
+ export class DocumentChangedError extends Error {
91
+ constructor(label) {
92
+ super(`${label} changed after it was prepared; prepare again`);
93
+ this.name = 'DocumentChangedError';
94
+ }
95
+ }
96
+ const sha256Of = (bytes) => `sha256:${sha256Hex(bytes)}`;
97
+ /** `sha256:<hex>` of the regular file at `filePath`, or `null` when it does not exist. */
98
+ async function fileSha256OrNull(filePath, label, maxBytes) {
99
+ try {
100
+ return sha256Of(await readRegularFile(filePath, label, maxBytes));
101
+ }
102
+ catch (error) {
103
+ if (error?.code === 'ENOENT')
104
+ return null;
105
+ throw error;
106
+ }
107
+ }
108
+ const FORBIDDEN_WHY = {
109
+ root: 'the root key',
110
+ recovery: 'a recovery key',
111
+ timestamp: 'the timestamp key',
112
+ retired: 'a key that held the root or timestamp role',
113
+ formerRecovery: 'a former recovery key',
114
+ revoked: 'a revoked key',
115
+ };
116
+ /** Reads the trust root once, applies the change and returns the unsigned result. Never signs or writes. */
117
+ export async function prepareTrustRootChange(o) {
118
+ const change = o.change;
119
+ let approverEntry = null;
120
+ const issuedAt = truncateToSecond(o.now);
121
+ if (change.kind === 'add') {
122
+ const approverName = requireString(change.name, 'Approver name', MAX_APPROVER_NAME_LEN);
123
+ if (!isDisplaySafeText(approverName))
124
+ throw new Error('Approver name contains characters that cannot be shown safely');
125
+ if (change.scopes.length === 0)
126
+ throw new Error('At least one --scope is required');
127
+ const scopes = [...new Set(change.scopes.map((scope) => validateScope(scope)))].sort();
128
+ if (scopes.length > MAX_SCOPES)
129
+ throw new Error(`Approver scopes cannot exceed ${MAX_SCOPES}`);
130
+ approverEntry = {
131
+ ...change.publicKey,
132
+ name: approverName,
133
+ scopes,
134
+ expiresAt: addDuration(issuedAt, change.expires, 'approver expiry'),
135
+ };
136
+ }
137
+ const removeId = change.kind === 'remove' ? validateKeyId(change.keyId, 'Approver key id') : null;
138
+ const source = o.trustRootPath;
139
+ const rawBytes = await readRegularFile(source, 'Trust root', MAX_TRUST_ROOT_BYTES);
140
+ const current = readTrustRootForUpdate(rawBytes, o.rootKeyId);
141
+ if (o.minimumVersion !== undefined && current.version < o.minimumVersion)
142
+ throw new StaleDocumentError('Trust root', current.version, o.minimumVersion);
143
+ const expiresAt = change.trustRootExpires
144
+ ? addDuration(issuedAt, change.trustRootExpires, 'trust root expiry')
88
145
  : current.expiresAt;
89
146
  const next = {
90
147
  ...current,
91
148
  version: current.version + 1,
92
149
  issuedAt,
93
150
  expiresAt,
94
- approvers: current.approvers.map((approver) => ({
95
- ...approver,
96
- scopes: [...approver.scopes],
97
- })),
151
+ approvers: current.approvers.map((approver) => ({ ...approver, scopes: [...approver.scopes] })),
98
152
  };
99
- change(next);
100
- if (next.expiresAt <= issuedAt) {
153
+ let replaces = null;
154
+ if (change.kind === 'add') {
155
+ if (change.publicKey.keyId === next.root.keyId)
156
+ throw new Error('The root key cannot be an approver; generate a separate approver key');
157
+ replaces = next.approvers.find((entry) => entry.keyId === change.publicKey.keyId) ?? null;
158
+ const remaining = next.approvers.filter((entry) => entry.keyId !== change.publicKey.keyId);
159
+ if (remaining.length >= MAX_APPROVERS)
160
+ throw new Error(`Trust root cannot exceed ${MAX_APPROVERS} approvers`);
161
+ remaining.push(approverEntry);
162
+ next.approvers = remaining;
163
+ }
164
+ else if (change.kind === 'remove') {
165
+ const beforeCount = next.approvers.length;
166
+ next.approvers = next.approvers.filter((entry) => entry.keyId !== removeId);
167
+ if (next.approvers.length === beforeCount)
168
+ throw new Error(`No approver with key id ${removeId}`);
169
+ }
170
+ if (next.expiresAt <= issuedAt)
101
171
  throw new Error('The trust root would already be expired; pass --expires to extend it');
172
+ for (const entry of o.forbidden ?? []) {
173
+ if (next.approvers.some((approver) => approver.keyId === entry.keyId))
174
+ throw new Error(`Approver ${entry.keyId} cannot stay or be added: it is ${FORBIDDEN_WHY[entry.why]}`);
102
175
  }
103
- const targetPath = options.out ?? options.trustRootPath;
104
- const written = await writeFileAtomic(targetPath, signTrustRoot(next, root.privateKey));
105
- return { version: next.version, path: written, trustRoot: next };
176
+ const target = o.out ?? source;
177
+ return {
178
+ kind: 'trust-root',
179
+ source,
180
+ target,
181
+ baseSha256: sha256Of(rawBytes),
182
+ targetBase: path.resolve(target) === path.resolve(source) ? null : await fileSha256OrNull(target, 'Trust root output', MAX_TRUST_ROOT_BYTES),
183
+ rootKeyId: o.rootKeyId,
184
+ current,
185
+ payload: next,
186
+ change,
187
+ replaces,
188
+ };
189
+ }
190
+ function requireKey(p, key) {
191
+ if (key.keyId !== p.rootKeyId)
192
+ throw new Error('Key does not match the prepared root key');
193
+ }
194
+ /** Pure: signs `p.payload` and nothing else. The preview and commit use this one code path. */
195
+ export function signPreparedTrustRoot(p, key) {
196
+ return signTrustRoot(p.payload, key.privateKey);
197
+ }
198
+ export async function commitTrustRoot(p, rootKey, o = {}) {
199
+ requireKey(p, rootKey);
200
+ const again = await readRegularFile(p.source, 'Trust root', MAX_TRUST_ROOT_BYTES);
201
+ if (sha256Of(again) !== p.baseSha256)
202
+ throw new DocumentChangedError('Trust root');
203
+ const bytes = signPreparedTrustRoot(p, rootKey);
204
+ await o.beforeWrite?.();
205
+ const sameFile = path.resolve(p.target) === path.resolve(p.source);
206
+ const written = await writeFileAtomic(p.target, bytes, 0o644, { expectSha256: sameFile ? p.baseSha256 : p.targetBase });
207
+ return { version: p.payload.version, path: written, sha256: sha256Of(bytes) };
208
+ }
209
+ async function updateTrustRoot(options, change, forbidden) {
210
+ const root = await loadPrivateKey(options.rootKeyPath);
211
+ const prepared = await prepareTrustRootChange({
212
+ rootKeyId: root.keyId, trustRootPath: options.trustRootPath, out: options.out, now: options.now,
213
+ change: change(options.expires), forbidden,
214
+ });
215
+ const written = await commitTrustRoot(prepared, root);
216
+ return { version: written.version, path: written.path, trustRoot: prepared.payload };
106
217
  }
107
218
  export async function addApprover(options) {
108
- const approverName = requireString(options.name, 'Approver name', MAX_APPROVER_NAME_LEN);
219
+ requireString(options.name, 'Approver name', MAX_APPROVER_NAME_LEN);
109
220
  const approverKey = await loadPublicKey(options.publicKeyPath);
110
- if (options.scopes.length === 0) {
111
- throw new Error('At least one --scope is required');
112
- }
113
- const scopes = [...new Set(options.scopes.map((scope) => validateScope(scope)))].sort();
114
- if (scopes.length > MAX_SCOPES) {
115
- throw new Error(`Approver scopes cannot exceed ${MAX_SCOPES}`);
116
- }
117
- const issuedAt = truncateToSecond(options.now);
118
- const expiresAt = addDuration(issuedAt, options.expires, 'approver expiry');
119
- const result = await updateTrustRoot({
120
- rootKeyPath: options.rootKeyPath,
121
- trustRootPath: options.trustRootPath,
122
- out: options.out,
123
- now: options.now,
124
- expires: options.trustRootExpires,
125
- }, (trustRoot) => {
126
- if (approverKey.keyId === trustRoot.root.keyId) {
127
- throw new Error('The root key cannot be an approver; generate a separate approver key');
128
- }
129
- const remaining = trustRoot.approvers.filter((entry) => entry.keyId !== approverKey.keyId);
130
- if (remaining.length >= MAX_APPROVERS) {
131
- throw new Error(`Trust root cannot exceed ${MAX_APPROVERS} approvers`);
132
- }
133
- remaining.push({
134
- ...approverKey,
135
- name: approverName,
136
- scopes,
137
- expiresAt,
138
- });
139
- trustRoot.approvers = remaining;
140
- });
221
+ const result = await updateTrustRoot({ ...options, expires: options.trustRootExpires }, (trustRootExpires) => ({
222
+ kind: 'add', publicKey: approverKey, name: options.name, scopes: options.scopes, expires: options.expires,
223
+ ...(trustRootExpires ? { trustRootExpires } : {}),
224
+ }), options.forbidden);
141
225
  return { version: result.version, path: result.path, keyId: approverKey.keyId };
142
226
  }
143
227
  export async function removeApprover(options) {
144
228
  const targetKeyId = validateKeyId(options.keyId, 'Approver key id');
145
- return await updateTrustRoot({
146
- rootKeyPath: options.rootKeyPath,
147
- trustRootPath: options.trustRootPath,
148
- out: options.out,
149
- now: options.now,
150
- expires: options.trustRootExpires,
151
- }, (trustRoot) => {
152
- const beforeCount = trustRoot.approvers.length;
153
- trustRoot.approvers = trustRoot.approvers.filter((entry) => entry.keyId !== targetKeyId);
154
- if (trustRoot.approvers.length === beforeCount) {
155
- throw new Error(`No approver with key id ${targetKeyId}`);
156
- }
157
- });
229
+ return await updateTrustRoot({ ...options, expires: options.trustRootExpires }, (trustRootExpires) => ({ kind: 'remove', keyId: targetKeyId, ...(trustRootExpires ? { trustRootExpires } : {}) }));
158
230
  }
159
- export async function updateRevocations(options) {
160
- const root = await loadPrivateKey(options.rootKeyPath);
161
- const rawBytes = await readRegularFile(options.revocationsPath, 'Revocation list', MAX_REVOCATIONS_BYTES);
162
- const current = readRevocationsForUpdate(rawBytes, root.keyId, root.publicKey);
163
- const issuedAt = truncateToSecond(options.now);
164
- const expiresAt = addDuration(issuedAt, options.expires ?? DEFAULT_REVOCATIONS_EXPIRY, 'revocation list expiry');
165
- const reason = requireString(options.reason ?? DEFAULT_REVOKE_REASON, 'Revocation reason', MAX_REVOCATION_REASON);
231
+ /** Re-signs the trust root with a new expiry and the same approvers. */
232
+ export async function renewTrustRoot(options) {
233
+ const result = await updateTrustRoot({ ...options, expires: options.expires }, (expires) => ({ kind: 'renew', trustRootExpires: expires }));
234
+ return { version: result.version, path: result.path, expiresAt: result.trustRoot.expiresAt };
235
+ }
236
+ export async function prepareRevocationsChange(o) {
237
+ const change = o.change;
238
+ const issuedAt = truncateToSecond(o.now);
239
+ let reason = '';
240
+ if (change.kind !== 'refresh') {
241
+ reason = requireString(change.reason, 'Revocation reason', MAX_REVOCATION_REASON);
242
+ if (!isDisplaySafeText(reason))
243
+ throw new Error('Revocation reason contains characters that cannot be shown safely');
244
+ }
245
+ const keyId = change.kind === 'key' || change.kind === 'both' ? validateKeyId(change.keyId, 'Revoked key id') : null;
246
+ const digest = change.kind === 'digest' || change.kind === 'both' ? validateDigest(change.digest) : null;
247
+ const source = o.revocationsPath;
248
+ const rawBytes = await readRegularFile(source, 'Revocation list', MAX_REVOCATIONS_BYTES);
249
+ const current = readRevocationsForUpdate(rawBytes, o.rootKeyId, o.rootPublicKey);
250
+ if (o.minimumVersion !== undefined && current.version < o.minimumVersion)
251
+ throw new StaleDocumentError('Revocation list', current.version, o.minimumVersion);
252
+ const expiresAt = addDuration(issuedAt, o.expires, 'revocation list expiry');
166
253
  const next = {
167
254
  ...current,
168
255
  version: current.version + 1,
@@ -171,31 +258,76 @@ export async function updateRevocations(options) {
171
258
  keys: [...current.keys],
172
259
  digests: [...current.digests],
173
260
  };
174
- if (options.revokeKey !== undefined) {
175
- const keyId = validateKeyId(options.revokeKey, 'Revoked key id');
176
- if (next.keys.some((entry) => entry.keyId === keyId)) {
261
+ if (keyId !== null) {
262
+ if (next.keys.some((entry) => entry.keyId === keyId))
177
263
  throw new Error(`Key ${keyId} is already revoked`);
178
- }
179
264
  next.keys.push({ keyId, reason, revokedAt: issuedAt });
180
265
  }
181
- if (options.revokeDigest !== undefined) {
182
- const digest = validateDigest(options.revokeDigest);
183
- if (next.digests.some((entry) => entry.digest === digest)) {
266
+ if (digest !== null) {
267
+ if (next.digests.some((entry) => entry.digest === digest))
184
268
  throw new Error(`Digest ${digest} is already revoked`);
185
- }
186
269
  next.digests.push({ digest, reason, revokedAt: issuedAt });
187
270
  }
188
- const targetPath = options.out ?? options.revocationsPath;
189
- const written = await writeFileAtomic(targetPath, signRevocations(next, root.privateKey));
190
- return { version: next.version, path: written, expiresAt: next.expiresAt };
271
+ const target = o.out ?? source;
272
+ return {
273
+ kind: 'revocations',
274
+ source,
275
+ target,
276
+ baseSha256: sha256Of(rawBytes),
277
+ targetBase: path.resolve(target) === path.resolve(source) ? null : await fileSha256OrNull(target, 'Revocation list output', MAX_REVOCATIONS_BYTES),
278
+ rootKeyId: o.rootKeyId,
279
+ current,
280
+ payload: next,
281
+ change,
282
+ };
283
+ }
284
+ export function signPreparedRevocations(p, key) {
285
+ return signRevocations(p.payload, key.privateKey);
286
+ }
287
+ export async function commitRevocations(p, rootKey, o = {}) {
288
+ requireKey(p, rootKey);
289
+ const again = await readRegularFile(p.source, 'Revocation list', MAX_REVOCATIONS_BYTES);
290
+ if (sha256Of(again) !== p.baseSha256)
291
+ throw new DocumentChangedError('Revocation list');
292
+ const bytes = signPreparedRevocations(p, rootKey);
293
+ await o.beforeWrite?.();
294
+ const sameFile = path.resolve(p.target) === path.resolve(p.source);
295
+ const written = await writeFileAtomic(p.target, bytes, 0o644, { expectSha256: sameFile ? p.baseSha256 : p.targetBase });
296
+ return { version: p.payload.version, path: written, sha256: sha256Of(bytes), expiresAt: p.payload.expiresAt };
297
+ }
298
+ export async function updateRevocations(options) {
299
+ const root = await loadPrivateKey(options.rootKeyPath);
300
+ const reason = requireString(options.reason ?? DEFAULT_REVOKE_REASON, 'Revocation reason', MAX_REVOCATION_REASON);
301
+ const change = options.revokeKey !== undefined && options.revokeDigest !== undefined ? { kind: 'both', keyId: options.revokeKey, digest: options.revokeDigest, reason }
302
+ : options.revokeKey !== undefined ? { kind: 'key', keyId: options.revokeKey, reason }
303
+ : options.revokeDigest !== undefined ? { kind: 'digest', digest: options.revokeDigest, reason }
304
+ : { kind: 'refresh' };
305
+ const prepared = await prepareRevocationsChange({
306
+ rootKeyId: root.keyId, rootPublicKey: root.publicKey, revocationsPath: options.revocationsPath, out: options.out,
307
+ now: options.now, expires: options.expires ?? DEFAULT_REVOCATIONS_EXPIRY, change,
308
+ });
309
+ const written = await commitRevocations(prepared, root);
310
+ return { version: written.version, path: written.path, expiresAt: written.expiresAt };
191
311
  }
192
312
  export async function approveSkill(options) {
193
313
  const skillDir = path.resolve(options.skillDir);
194
314
  const expected = options.expectDigest === undefined ? null : validateDigest(options.expectDigest, 'expected digest');
315
+ if ((options.approverKeyPath === undefined) === (options.approverKey === undefined)) {
316
+ throw new Error('Pass exactly one of approverKeyPath and approverKey');
317
+ }
318
+ if (options.out !== undefined && options.outDir !== undefined) {
319
+ throw new Error('Pass at most one of out and outDir');
320
+ }
195
321
  await requireSkillRoot(skillDir);
196
- // Verify the key path is outside the Skill directory before reading the private key.
197
- await resolveOutside(options.approverKeyPath, skillDir, 'Approver private key');
198
- const approver = await loadPrivateKey(options.approverKeyPath);
322
+ let approver;
323
+ if (options.approverKeyPath !== undefined) {
324
+ // Verify the key path is outside the Skill directory before reading the private key.
325
+ await resolveOutside(options.approverKeyPath, skillDir, 'Approver private key');
326
+ approver = await loadPrivateKey(options.approverKeyPath);
327
+ }
328
+ else {
329
+ approver = options.approverKey;
330
+ }
199
331
  const tree = await collectSkillTree(skillDir, { includeContents: true });
200
332
  // Taken from the same SKILL.md bytes that are hashed and signed, so the name cannot differ from the reviewed version.
201
333
  const skillName = skillNameFromSkillFile(skillDir, tree.contents.get(SKILL_FILENAME), options.name);
@@ -203,6 +335,9 @@ export async function approveSkill(options) {
203
335
  if (expected !== null && tree.digest !== expected) {
204
336
  throw new Error(`Refusing to approve: the Skill changed since it was reviewed (expected ${expected}, found ${tree.digest}). Review the current version and approve again.`);
205
337
  }
338
+ if (options.expectName !== undefined && skillName !== options.expectName) {
339
+ throw new Error(`Refusing to approve: the Skill name is "${skillName}", not the reviewed "${options.expectName}"`);
340
+ }
206
341
  for (const [filePath, content] of tree.contents) {
207
342
  if (containsPrivateKey(content)) {
208
343
  throw new Error(`Refusing to approve: ${filePath} contains a private key. Remove it from the Skill and rotate that key.`);
@@ -230,7 +365,9 @@ export async function approveSkill(options) {
230
365
  reason: options.reason,
231
366
  previousDigest,
232
367
  });
233
- const targetPath = options.out ?? path.join(tree.root, CERTIFICATE_FILENAME);
368
+ await options.beforeSign?.({ skillName, digest: tree.digest });
369
+ const targetPath = options.out
370
+ ?? (options.outDir === undefined ? path.join(tree.root, CERTIFICATE_FILENAME) : path.join(options.outDir, `${skillName}.cert.json`));
234
371
  const written = await writeFileAtomic(targetPath, bytes);
235
372
  const parsed = parseCertificate(bytes);
236
373
  return {