@chatpanel/events 0.5.0 → 0.6.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.
package/index.js CHANGED
@@ -43,5 +43,6 @@ export { createManifest, ManifestError, SOURCES } from './manifest.js';
43
43
  export { createKernel, meetDecisions, KernelError, REQUIRED_PLUGINS, ALLOW_ALL } from './kernel.js';
44
44
  export { replay, formatReport, parseJsonl, toJsonl } from './harness.js';
45
45
  export { compileQuery, findMatches, matchIndexFor, expandReplacement, replaceMatch, replaceAll, replaceAllInRange, MAX_MATCHES } from './text-search.js';
46
+ export { SKILL_MANIFEST_VERSION, SKILL_CONTEXTS, SKILL_HISTORY_SCOPES, SKILL_MCP_MODES, SKILL_TRUST, SKILL_FILE_KINDS, SKILL_UPCASTERS, SkillManifestError, isSafeSkillPath, originOf, trustOf, skillFiles, needsBridge, declaredAccess, originLabel, sameSkillOrigin, skillIsStale, validateSkill, upcastSkill, upcastSkills, normalizeSkill } from './skill-manifest.js';
46
47
  export { SKILL_VARS, SKILL_VAR_NAMES, skillVar, skillVarPattern, parseSkillVars, lintSkillPrompt, suggestSkillVar, substituteSkillVars, skillVarGuidance, SkillVarError } from './skill-vars.js';
47
48
  export { outlineOf, parseListItem, continueList, indentSelection, toggleWrap, toggleLinePrefix, toggleTask, toggleLink, docStats, selectionStats } from './markdown-authoring.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatpanel/events",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "The canonical ChatPanel event-log and capability contracts \u2014 typed durable facts, clock-free deterministic linearization, schema upcasting, and the invariants the replay harness asserts. Pure, dependency-free ESM shared by the ChatPanel extension, gateway and bridge.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -25,6 +25,7 @@
25
25
  "./router.js": "./router.js",
26
26
  "./rules.js": "./rules.js",
27
27
  "./search-engines.js": "./search-engines.js",
28
+ "./skill-manifest.js": "./skill-manifest.js",
28
29
  "./skill-vars.js": "./skill-vars.js",
29
30
  "./sources-retrieval.js": "./sources-retrieval.js",
30
31
  "./sources.js": "./sources.js",
@@ -58,6 +59,7 @@
58
59
  "router.js",
59
60
  "rules.js",
60
61
  "search-engines.js",
62
+ "skill-manifest.js",
61
63
  "skill-vars.js",
62
64
  "sources-retrieval.js",
63
65
  "sources.js",
@@ -0,0 +1,330 @@
1
+ // skill-manifest.js — what a skill IS, for every client that stores or runs one.
2
+ //
3
+ // A skill was a settings record: a prompt plus a few dropdowns, normalized by whichever
4
+ // client happened to save it. That was survivable while every skill was hand-written by
5
+ // the person running it. It stops being survivable the moment a skill can ARRIVE from
6
+ // somewhere — a hub, a repo, another agent's skill directory — because the questions
7
+ // change from "what does this prompt say" to "where did it come from, what may it reach,
8
+ // and has anything checked it".
9
+ //
10
+ // So this module owns the record. Not the storage (a platform question), not the UI (a
11
+ // platform question), not the fetching (F6 S3) — the shape, its evolution, and the two
12
+ // derivations no client may compute for itself:
13
+ //
14
+ // • TRUST IS DERIVED, NEVER DECLARED. A skill cannot say it is trusted. `trustOf()`
15
+ // reads provenance: shipped by us -> 'built-in', fetched -> 'community', neither ->
16
+ // 'user'. A stored `trust` field is stripped on normalize, and `builtin` is forced
17
+ // false whenever an origin is present — otherwise "trusted" would be a string an
18
+ // importer sets.
19
+ // • DECLARED ACCESS IS COMPUTED FROM THE RECORD. Everything a skill can reach is
20
+ // readable before it runs, which is what makes load-time approval possible (P10).
21
+ // If a reviewer's summary were assembled by the install screen, the install screen
22
+ // would be the security boundary.
23
+ //
24
+ // Versioning follows the log's discipline: additive only, absence means the previous
25
+ // default, and the upcast chain exists from the start so adding v3 does not mean
26
+ // rewriting every reader.
27
+
28
+ import { DATA_SCOPES } from './capability.js';
29
+
30
+ export class SkillManifestError extends Error {
31
+ constructor(code, message) { super(message); this.name = 'SkillManifestError'; this.code = code; }
32
+ }
33
+
34
+ /** Schema version of the RECORD. Distinct from `version`, which is the author's semver. */
35
+ export const SKILL_MANIFEST_VERSION = 2;
36
+
37
+ export const SKILL_CONTEXTS = Object.freeze(['auto', 'page', 'selection', 'tabs', 'none']);
38
+ export const SKILL_HISTORY_SCOPES = Object.freeze(['none', 'chats', 'meetings', 'all']);
39
+ export const SKILL_MCP_MODES = Object.freeze(['none', 'selected', 'default']);
40
+
41
+ /**
42
+ * Derived, never stored. 'community' is deliberately the level for a skill fetched from
43
+ * a vendor repo with a famous name — only what we ship went through our review.
44
+ */
45
+ export const SKILL_TRUST = Object.freeze(['built-in', 'user', 'community']);
46
+
47
+ /**
48
+ * The directories a package may carry, matching the agentskills.io layout. `scripts` is
49
+ * the tier-3 one: it cannot run in a browser extension and must not be offered as if it
50
+ * could (see `needsBridge`).
51
+ */
52
+ export const SKILL_FILE_KINDS = Object.freeze(['references', 'scripts', 'assets', 'templates', 'examples']);
53
+
54
+ const str = (v) => typeof v === 'string' && v.length > 0;
55
+
56
+ // A slash command is typed by a human and matched case-insensitively against a stored
57
+ // string; anything outside this grammar either cannot be typed or collides with the
58
+ // leading-slash parse.
59
+ const COMMAND = /^[a-z0-9][a-z0-9_-]*$/;
60
+
61
+ /**
62
+ * A file path inside a package directory.
63
+ *
64
+ * The check is a security boundary, not tidiness: these strings become filesystem paths
65
+ * in the bridge's skill store, and a package is authored by a stranger. Rejected are
66
+ * absolute paths, any `..` segment, backslashes (a Windows separator that a POSIX
67
+ * `split('/')` would not see as one), leading/trailing whitespace and control characters.
68
+ * Allowing one of them is a directory-traversal write on someone's machine.
69
+ */
70
+ export function isSafeSkillPath(p) {
71
+ if (!str(p) || p.length > 255) return false;
72
+ if (p !== p.trim()) return false;
73
+ if (p.includes('\\')) return false;
74
+ // eslint-disable-next-line no-control-regex
75
+ if (/[\u0000-\u001f\u007f]/.test(p)) return false;
76
+ if (p.startsWith('/') || /^[a-z]:/i.test(p)) return false;
77
+ const parts = p.split('/');
78
+ if (parts.some((seg) => seg === '' || seg === '.' || seg === '..')) return false;
79
+ return true;
80
+ }
81
+
82
+ /** Did this skill come from somewhere, or did the user write it? */
83
+ export function originOf(skill) {
84
+ const o = skill?.origin;
85
+ return o && typeof o === 'object' && str(o.source) && str(o.id) ? o : null;
86
+ }
87
+
88
+ /**
89
+ * Derived from provenance, never read from the record. A skill that arrived from outside
90
+ * is 'community' whoever published it; 'built-in' means we shipped it.
91
+ */
92
+ export function trustOf(skill) {
93
+ if (originOf(skill)) return 'community';
94
+ return skill?.builtin ? 'built-in' : 'user';
95
+ }
96
+
97
+ /** The files a package carries, per directory, already filtered to safe paths. */
98
+ export function skillFiles(skill) {
99
+ const files = skill?.files;
100
+ const out = {};
101
+ if (!files || typeof files !== 'object') return out;
102
+ for (const kind of SKILL_FILE_KINDS) {
103
+ const list = Array.isArray(files[kind]) ? files[kind].filter(isSafeSkillPath) : [];
104
+ if (list.length) out[kind] = [...new Set(list)];
105
+ }
106
+ return out;
107
+ }
108
+
109
+ /**
110
+ * True when running this skill fully needs a host that can execute code — i.e. the
111
+ * bridge. The extension may still store, show and run the PROMPT half; what it may not
112
+ * do is pretend the scripts ran.
113
+ */
114
+ export function needsBridge(skill) {
115
+ return (skillFiles(skill).scripts || []).length > 0;
116
+ }
117
+
118
+ /**
119
+ * Everything this skill can reach, computed from the record. This is what an install
120
+ * review, the Plugins lens and an admin export all read — one derivation, so the three
121
+ * cannot disagree about what was approved.
122
+ */
123
+ export function declaredAccess(skill = {}) {
124
+ const files = skillFiles(skill);
125
+ const mcpMode = SKILL_MCP_MODES.includes(skill.mcpMode) ? skill.mcpMode : 'none';
126
+ const context = SKILL_CONTEXTS.includes(skill.context) ? skill.context : 'auto';
127
+ const history = SKILL_HISTORY_SCOPES.includes(skill.historyContext) ? skill.historyContext : 'none';
128
+ const reads = new Set(Array.isArray(skill.reads) ? skill.reads.filter((r) => DATA_SCOPES.includes(r)) : []);
129
+ // The dropdowns ARE access statements; a record that declared `reads` separately from
130
+ // them could claim less than it takes.
131
+ if (context !== 'none') reads.add('page');
132
+ if (history === 'chats' || history === 'all') reads.add('chats');
133
+ if (history === 'meetings' || history === 'all') reads.add('meetings');
134
+ return {
135
+ trust: trustOf(skill),
136
+ reads: [...reads].sort(),
137
+ page: context,
138
+ history,
139
+ mcp: mcpMode,
140
+ mcpServerIds: mcpMode === 'selected' && Array.isArray(skill.mcpServerIds) ? [...skill.mcpServerIds] : [],
141
+ sources: Array.isArray(skill.sources) ? [...new Set(skill.sources.filter(str))].sort() : [],
142
+ surfaces: Array.isArray(skill.surfaces) ? [...new Set(skill.surfaces.filter(str))].sort() : [],
143
+ scripts: files.scripts || [],
144
+ needsBridge: needsBridge(skill),
145
+ meeting: !!skill.meeting,
146
+ };
147
+ }
148
+
149
+ /** Human label for where a skill came from, for the surfaces that must show provenance. */
150
+ export function originLabel(skill) {
151
+ const o = originOf(skill);
152
+ if (!o) return trustOf(skill) === 'built-in' ? 'Built-in' : 'Written here';
153
+ return o.id.length > 48 ? `${o.source} · …${o.id.slice(-40)}` : `${o.source} · ${o.id}`;
154
+ }
155
+
156
+ /** Two records describing the same upstream skill — for update checks and dedupe. */
157
+ export function sameSkillOrigin(a, b) {
158
+ const x = originOf(a);
159
+ const y = originOf(b);
160
+ return !!x && !!y && x.source === y.source && x.id === y.id;
161
+ }
162
+
163
+ /**
164
+ * Has upstream changed since this was installed? `null` when unanswerable (no origin, or
165
+ * nothing was hashed) — which callers must treat as "do not claim it is current" rather
166
+ * than as "up to date".
167
+ */
168
+ export function skillIsStale(skill, upstreamHash) {
169
+ const o = originOf(skill);
170
+ if (!o || !str(o.hash) || !str(upstreamHash)) return null;
171
+ return o.hash !== upstreamHash;
172
+ }
173
+
174
+ /**
175
+ * Validate a skill DECLARATION — the static surface approved before anything runs.
176
+ * Throws on the first problem, like `validateCapability`.
177
+ */
178
+ export function validateSkill(skill) {
179
+ if (!skill || typeof skill !== 'object') throw new SkillManifestError('SHAPE', 'skill must be an object');
180
+ if (!str(skill.id)) throw new SkillManifestError('SHAPE', 'skill.id required');
181
+ if (!str(skill.name)) throw new SkillManifestError('SHAPE', `skill '${skill.id}': name required`);
182
+ if (skill.prompt != null && typeof skill.prompt !== 'string') {
183
+ throw new SkillManifestError('SHAPE', `skill '${skill.id}': prompt must be a string`);
184
+ }
185
+ if (skill.command != null && skill.command !== '' && !COMMAND.test(String(skill.command))) {
186
+ throw new SkillManifestError('SHAPE', `skill '${skill.id}': command must match ${COMMAND}`);
187
+ }
188
+ for (const [field, allowed] of [
189
+ ['context', SKILL_CONTEXTS], ['historyContext', SKILL_HISTORY_SCOPES], ['mcpMode', SKILL_MCP_MODES],
190
+ ]) {
191
+ if (skill[field] != null && !allowed.includes(skill[field])) {
192
+ throw new SkillManifestError('SHAPE', `skill '${skill.id}': ${field} must be one of ${allowed}`);
193
+ }
194
+ }
195
+ if (skill.reads != null) {
196
+ if (!Array.isArray(skill.reads) || !skill.reads.every((r) => DATA_SCOPES.includes(r))) {
197
+ throw new SkillManifestError('SHAPE', `skill '${skill.id}': reads must be within ${DATA_SCOPES}`);
198
+ }
199
+ }
200
+ for (const field of ['sources', 'surfaces']) {
201
+ if (skill[field] != null && (!Array.isArray(skill[field]) || !skill[field].every(str))) {
202
+ throw new SkillManifestError('SHAPE', `skill '${skill.id}': ${field} must be an array of ids`);
203
+ }
204
+ }
205
+ if (skill.origin != null) {
206
+ const o = skill.origin;
207
+ if (typeof o !== 'object') throw new SkillManifestError('SHAPE', `skill '${skill.id}': origin must be an object`);
208
+ // Without a source and an id it cannot be re-fetched or compared, which is the whole
209
+ // reason the field exists; a half-origin is worse than none because it looks answered.
210
+ if (!str(o.source) || !str(o.id)) {
211
+ throw new SkillManifestError('ORIGIN', `skill '${skill.id}': origin needs both source and id`);
212
+ }
213
+ }
214
+ if (skill.files != null) {
215
+ if (typeof skill.files !== 'object') throw new SkillManifestError('SHAPE', `skill '${skill.id}': files must be an object`);
216
+ for (const kind of Object.keys(skill.files)) {
217
+ if (!SKILL_FILE_KINDS.includes(kind)) {
218
+ throw new SkillManifestError('FILES', `skill '${skill.id}': unknown file kind '${kind}'`);
219
+ }
220
+ const list = skill.files[kind];
221
+ if (!Array.isArray(list) || !list.every(str)) {
222
+ throw new SkillManifestError('FILES', `skill '${skill.id}': files.${kind} must be an array of paths`);
223
+ }
224
+ const bad = list.find((p) => !isSafeSkillPath(p));
225
+ if (bad) throw new SkillManifestError('PATH', `skill '${skill.id}': unsafe path in files.${kind}: ${JSON.stringify(bad)}`);
226
+ }
227
+ }
228
+ // Files without an origin means someone hand-wrote a package record; that is allowed,
229
+ // but scripts without an origin cannot be scanned against anything, and an unscannable
230
+ // script is exactly what the admission gate exists to refuse.
231
+ if (!originOf(skill) && (skill.files?.scripts || []).length) {
232
+ throw new SkillManifestError('ORIGIN', `skill '${skill.id}': scripts require an origin to be scannable`);
233
+ }
234
+ return skill;
235
+ }
236
+
237
+ /**
238
+ * v(n) -> v(n+1). Each MUST be total: it may not throw for any record of its version.
239
+ *
240
+ * v1 -> v2 stamps the version and nothing else, on purpose. Every field F6 adds is
241
+ * absence-means-the-old-default, so there is nothing to fill in — the same discipline as
242
+ * storing which plugins are DISABLED rather than the full state, so a field added later
243
+ * needs no migration.
244
+ */
245
+ export const SKILL_UPCASTERS = Object.freeze({
246
+ 1: (s) => ({ ...s, v: SKILL_MANIFEST_VERSION }),
247
+ });
248
+
249
+ /** Carry a stored skill forward to the current schema. Pure; never mutates the input. */
250
+ export function upcastSkill(stored) {
251
+ if (!stored || typeof stored !== 'object') throw new SkillManifestError('SHAPE', 'skill must be an object');
252
+ // A record written before the version existed is v1 by definition.
253
+ let s = typeof stored.v === 'number' ? stored : { ...stored, v: 1 };
254
+ let guard = 0;
255
+ while (s.v < SKILL_MANIFEST_VERSION) {
256
+ const step = SKILL_UPCASTERS[s.v];
257
+ if (!step) throw new SkillManifestError('UPCAST', `no upcaster from v${s.v}`);
258
+ s = step(s);
259
+ if (++guard > 64) throw new SkillManifestError('UPCAST', 'upcaster chain did not terminate');
260
+ }
261
+ if (s.v > SKILL_MANIFEST_VERSION) {
262
+ throw new SkillManifestError('UPCAST', `skill is v${s.v}; this reader only knows v${SKILL_MANIFEST_VERSION}`);
263
+ }
264
+ return s;
265
+ }
266
+
267
+ export function upcastSkills(stored) {
268
+ return (Array.isArray(stored) ? stored : []).map(upcastSkill);
269
+ }
270
+
271
+ /**
272
+ * Coerce a record into the canonical shape a writer stores. Total and forgiving — this
273
+ * runs on save, where throwing would cost a user their edit; `validateSkill` is the
274
+ * strict gate, used where a record ARRIVES.
275
+ *
276
+ * The security-relevant coercions are the ones that cannot be left to a caller:
277
+ * `trust` is never persisted, `builtin` cannot survive an origin, and an unsafe file
278
+ * path is dropped rather than stored and rejected later.
279
+ */
280
+ export function normalizeSkill(skill) {
281
+ if (!skill || typeof skill !== 'object') return skill;
282
+ const out = { ...upcastSkill(skill) };
283
+
284
+ // Skills predate the enabled flag — absence means enabled.
285
+ out.enabled = out.enabled !== false;
286
+
287
+ const mode = String(out.mcpMode || 'none').toLowerCase();
288
+ out.mcpMode = SKILL_MCP_MODES.includes(mode) ? mode : 'none';
289
+ const ids = Array.isArray(out.mcpServerIds) ? out.mcpServerIds : [];
290
+ out.mcpServerIds = out.mcpMode === 'selected'
291
+ ? [...new Set(ids.map((id) => String(id || '').trim()).filter(Boolean))]
292
+ : [];
293
+
294
+ // Derived, never stored: a record that could assert its own trust would make every
295
+ // check downstream a formality.
296
+ delete out.trust;
297
+
298
+ const origin = originOf(out);
299
+ if (origin) {
300
+ out.builtin = false; // "we shipped it" is not something an import may claim
301
+ out.origin = {
302
+ source: origin.source,
303
+ id: origin.id,
304
+ ...(str(origin.url) ? { url: origin.url } : {}),
305
+ ...(str(origin.hash) ? { hash: origin.hash } : {}),
306
+ ...(origin.scanned && typeof origin.scanned === 'object' ? { scanned: { ...origin.scanned } } : {}),
307
+ };
308
+ } else if (out.origin != null) {
309
+ delete out.origin; // a half-origin looks answered and is not
310
+ }
311
+
312
+ if (out.reads != null) {
313
+ out.reads = [...new Set((Array.isArray(out.reads) ? out.reads : []).filter((r) => DATA_SCOPES.includes(r)))].sort();
314
+ }
315
+ for (const field of ['sources', 'surfaces']) {
316
+ if (out[field] != null) {
317
+ out[field] = [...new Set((Array.isArray(out[field]) ? out[field] : []).filter(str))];
318
+ }
319
+ }
320
+
321
+ if (out.files != null) {
322
+ const files = skillFiles(out);
323
+ if (Object.keys(files).length) out.files = files;
324
+ else delete out.files;
325
+ }
326
+
327
+ if (out.version != null && !str(out.version)) delete out.version;
328
+
329
+ return out;
330
+ }