@pugi/cli 0.1.0-alpha.10

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 (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +172 -0
  3. package/bin/run.js +2 -0
  4. package/dist/commands/jobs.js +245 -0
  5. package/dist/core/agents/loader.js +104 -0
  6. package/dist/core/agents/registry.js +69 -0
  7. package/dist/core/auto-open-browser.js +128 -0
  8. package/dist/core/bash-classifier.js +1001 -0
  9. package/dist/core/clipboard.js +70 -0
  10. package/dist/core/context/builder.js +114 -0
  11. package/dist/core/context/compaction-events.js +99 -0
  12. package/dist/core/context/compaction.js +602 -0
  13. package/dist/core/context/invariants.js +250 -0
  14. package/dist/core/context/markdown-loader.js +270 -0
  15. package/dist/core/credentials.js +355 -0
  16. package/dist/core/engine/adapter-runner.js +8 -0
  17. package/dist/core/engine/anvil-client.js +156 -0
  18. package/dist/core/engine/compaction-hook.js +154 -0
  19. package/dist/core/engine/index.js +12 -0
  20. package/dist/core/engine/native-pugi.js +369 -0
  21. package/dist/core/engine/noop.js +27 -0
  22. package/dist/core/engine/prompts.js +118 -0
  23. package/dist/core/engine/tool-bridge.js +313 -0
  24. package/dist/core/file-cache.js +29 -0
  25. package/dist/core/hooks.js +415 -0
  26. package/dist/core/index-store.js +260 -0
  27. package/dist/core/jobs/registry.js +462 -0
  28. package/dist/core/mcp/client.js +316 -0
  29. package/dist/core/mcp/registry.js +171 -0
  30. package/dist/core/mcp/trust.js +91 -0
  31. package/dist/core/path-security.js +63 -0
  32. package/dist/core/permission.js +309 -0
  33. package/dist/core/repl/cap-warning.js +91 -0
  34. package/dist/core/repl/clipboard-read.js +174 -0
  35. package/dist/core/repl/history-search.js +175 -0
  36. package/dist/core/repl/history.js +172 -0
  37. package/dist/core/repl/kill-ring.js +138 -0
  38. package/dist/core/repl/session.js +618 -0
  39. package/dist/core/repl/slash-commands.js +227 -0
  40. package/dist/core/repl/workspace-context.js +113 -0
  41. package/dist/core/session.js +258 -0
  42. package/dist/core/settings.js +59 -0
  43. package/dist/core/skills/loader.js +454 -0
  44. package/dist/core/skills/sources.js +480 -0
  45. package/dist/core/skills/trust.js +172 -0
  46. package/dist/core/subagents/dispatcher.js +258 -0
  47. package/dist/core/subagents/index.js +26 -0
  48. package/dist/core/subagents/spawn.js +86 -0
  49. package/dist/core/trust.js +109 -0
  50. package/dist/index.js +8 -0
  51. package/dist/runtime/cli.js +3405 -0
  52. package/dist/runtime/commands/agents.js +385 -0
  53. package/dist/runtime/commands/budget.js +192 -0
  54. package/dist/runtime/commands/config.js +231 -0
  55. package/dist/runtime/commands/privacy.js +107 -0
  56. package/dist/runtime/commands/skills.js +401 -0
  57. package/dist/runtime/commands/undo.js +329 -0
  58. package/dist/runtime/update-check.js +294 -0
  59. package/dist/tools/bash.js +660 -0
  60. package/dist/tools/file-tools.js +346 -0
  61. package/dist/tools/registry.js +25 -0
  62. package/dist/tools/web-fetch.js +535 -0
  63. package/dist/tui/agent-tree.js +66 -0
  64. package/dist/tui/conversation-pane.js +45 -0
  65. package/dist/tui/device-flow.js +142 -0
  66. package/dist/tui/input-box.js +474 -0
  67. package/dist/tui/login-picker.js +69 -0
  68. package/dist/tui/render.js +125 -0
  69. package/dist/tui/repl-render.js +240 -0
  70. package/dist/tui/repl-splash-art.js +64 -0
  71. package/dist/tui/repl-splash.js +111 -0
  72. package/dist/tui/repl.js +214 -0
  73. package/dist/tui/slash-palette.js +106 -0
  74. package/dist/tui/splash-data.js +61 -0
  75. package/dist/tui/splash.js +31 -0
  76. package/dist/tui/status-bar.js +71 -0
  77. package/dist/tui/update-banner.js +8 -0
  78. package/dist/tui/workspace-context.js +105 -0
  79. package/package.json +71 -0
@@ -0,0 +1,480 @@
1
+ import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, statSync, writeFileSync, } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { dirname, isAbsolute, join, resolve, sep } from 'node:path';
4
+ import { request } from 'undici';
5
+ import { validateHostnameForFetch } from '../../tools/web-fetch.js';
6
+ /**
7
+ * Skill / Agent source resolver.
8
+ *
9
+ * Translates a `<source>` argument from `pugi skills install <source>`
10
+ * into a temp directory containing the canonical layout we install
11
+ * from (`SKILL.md` for skills, `<name>.md` for agents).
12
+ *
13
+ * Supported source schemes:
14
+ *
15
+ * 1. `gh:owner/repo[/subdir][@ref]`
16
+ * → `gh:anthropics/skills/python-coding-standards@main`
17
+ * Fetches the GitHub tarball via the public codeload endpoint,
18
+ * extracts the requested subtree.
19
+ *
20
+ * 2. `https://github.com/<owner>/<repo>/tree/<ref>/<subdir>` (or `/blob/`)
21
+ * Normalised to the gh: form above.
22
+ *
23
+ * 3. `anthropic:<slug>` — convenience alias for
24
+ * `gh:anthropics/skills/<slug>@main`. Hard-coded base; the only
25
+ * reason this exists is so operators can copy a slug from the
26
+ * Anthropic docs without remembering the org name.
27
+ *
28
+ * 4. `npm:<package>` — fetches a tarball from the npm registry,
29
+ * extracts, looks for `SKILL.md` at the package root.
30
+ *
31
+ * 5. Local path — `./relative` or `/abs/path`. Copied to tmp so the
32
+ * caller can mutate the original without affecting install.
33
+ *
34
+ * 6. Catalog name — bare slug, queried against
35
+ * `https://catalog.pugi.dev/api/skills/<name>`. Returns a 404 →
36
+ * we surface a hint pointing at the `gh:anthropics/skills/<name>`
37
+ * form rather than crashing.
38
+ *
39
+ * Every resolver writes the payload into a fresh temp dir under
40
+ * `/tmp/pugi-skill-XXXXXX/` (caller cleans up after install completes).
41
+ * Network failures bubble up as `SOURCE_NETWORK` errors with the host
42
+ * + status code so the operator can diagnose firewall / proxy issues.
43
+ */
44
+ const ANTHROPIC_REPO = 'gh:anthropics/skills';
45
+ const CATALOG_BASE = process.env.PUGI_CATALOG_URL ?? 'https://catalog.pugi.dev';
46
+ const FETCH_TIMEOUT_MS = 30_000;
47
+ const MAX_PAYLOAD_BYTES = 50 * 1024 * 1024; // 50 MB cap on any single download
48
+ export async function fetchSource(source) {
49
+ if (source.startsWith('gh:')) {
50
+ return fetchGitHub(source.slice(3));
51
+ }
52
+ if (source.startsWith('https://github.com/') || source.startsWith('http://github.com/')) {
53
+ return fetchGitHub(normalizeGithubUrl(source));
54
+ }
55
+ if (source.startsWith('anthropic:')) {
56
+ const slug = source.slice('anthropic:'.length);
57
+ if (!slug || slug.includes('/')) {
58
+ throw new Error(`SOURCE_PARSE: anthropic: source needs a bare slug (got "${source}"). Example: anthropic:algorithmic-art`);
59
+ }
60
+ // Real layout is `anthropics/skills` repo → `skills/<slug>/SKILL.md`.
61
+ // Verified 2026-05-25 against the live repo tarball.
62
+ return fetchGitHub(`${ANTHROPIC_REPO}/skills/${slug}@main`.slice(3));
63
+ }
64
+ if (source.startsWith('npm:')) {
65
+ return fetchNpm(source.slice('npm:'.length));
66
+ }
67
+ if (source.startsWith('./') || source.startsWith('../') || isAbsolute(source)) {
68
+ return fetchLocal(source);
69
+ }
70
+ // Bare slug — try the catalog. Catalog might be down or the slug
71
+ // might not exist; fall through with a clear hint instead of crashing.
72
+ return fetchCatalog(source);
73
+ }
74
+ function normalizeGithubUrl(url) {
75
+ // https://github.com/<owner>/<repo>/tree/<ref>/<path...>
76
+ // https://github.com/<owner>/<repo>/blob/<ref>/<path...>
77
+ // https://github.com/<owner>/<repo>
78
+ const match = url.match(/^https?:\/\/github\.com\/([^/]+)\/([^/]+?)(?:\/(?:tree|blob)\/([^/]+)(?:\/(.+?))?)?(?:\.git)?\/?$/);
79
+ if (!match) {
80
+ throw new Error(`SOURCE_PARSE: cannot parse GitHub URL "${url}"`);
81
+ }
82
+ const [, owner, repo, ref, subdir] = match;
83
+ const ownerRepo = `${owner}/${repo}`;
84
+ const subPart = subdir ? `/${subdir}` : '';
85
+ const refPart = ref ? `@${ref}` : '';
86
+ return `${ownerRepo}${subPart}${refPart}`;
87
+ }
88
+ function parseGithubSpec(raw) {
89
+ // <owner>/<repo>[/<subdir>][@<ref>]
90
+ let ref = 'main';
91
+ let pathPart = raw;
92
+ const atIdx = raw.lastIndexOf('@');
93
+ if (atIdx > 0) {
94
+ ref = raw.slice(atIdx + 1);
95
+ pathPart = raw.slice(0, atIdx);
96
+ }
97
+ const segments = pathPart.split('/').filter((s) => s.length > 0);
98
+ if (segments.length < 2) {
99
+ throw new Error(`SOURCE_PARSE: gh: source needs owner/repo (got "${raw}"). Example: gh:anthropics/skills/python-coding-standards@main`);
100
+ }
101
+ const [owner, repo, ...subdirParts] = segments;
102
+ if (!owner || !repo) {
103
+ throw new Error(`SOURCE_PARSE: gh: source needs owner/repo (got "${raw}"). Example: gh:anthropics/skills/python-coding-standards@main`);
104
+ }
105
+ return {
106
+ owner,
107
+ repo,
108
+ subdir: subdirParts.join('/'),
109
+ ref,
110
+ };
111
+ }
112
+ async function fetchGitHub(raw) {
113
+ const spec = parseGithubSpec(raw);
114
+ // Use codeload.github.com — the public tarball endpoint requires no
115
+ // auth for public repos and returns a single .tar.gz of the requested
116
+ // ref's tree. Private repos are out of scope for α7.0.
117
+ const tarUrl = `https://codeload.github.com/${spec.owner}/${spec.repo}/tar.gz/${spec.ref}`;
118
+ const tmpRoot = mkdtempSync(join(tmpdir(), 'pugi-skill-gh-'));
119
+ const tarPath = join(tmpRoot, 'payload.tar.gz');
120
+ await downloadToFile(tarUrl, tarPath, `GitHub ${spec.owner}/${spec.repo}@${spec.ref}`);
121
+ const extractDir = join(tmpRoot, 'extract');
122
+ mkdirSync(extractDir, { recursive: true });
123
+ await extractTarball(tarPath, extractDir);
124
+ // GitHub tarballs unpack into `<repo>-<sanitised-ref>/` at the root.
125
+ const topLevel = readdirSync(extractDir);
126
+ if (topLevel.length !== 1) {
127
+ throw new Error(`SOURCE_TAR: expected a single root directory in tarball, got ${topLevel.length}`);
128
+ }
129
+ const rootName = topLevel[0];
130
+ if (!rootName) {
131
+ throw new Error('SOURCE_TAR: tarball root directory missing');
132
+ }
133
+ const repoRoot = join(extractDir, rootName);
134
+ const payloadRoot = spec.subdir ? join(repoRoot, spec.subdir) : repoRoot;
135
+ if (!existsSync(payloadRoot)) {
136
+ throw new Error(`SOURCE_PATH: subdirectory "${spec.subdir}" not found in ${spec.owner}/${spec.repo}@${spec.ref}`);
137
+ }
138
+ const sourceUrl = spec.subdir
139
+ ? `https://github.com/${spec.owner}/${spec.repo}/tree/${spec.ref}/${spec.subdir}`
140
+ : `https://github.com/${spec.owner}/${spec.repo}/tree/${spec.ref}`;
141
+ const inferredKind = inferKind(payloadRoot);
142
+ // Move payload into a stable directory inside tmpRoot for cleanup
143
+ // simplicity. The caller deletes tmpRoot when install completes.
144
+ // verbatimSymlinks: belt-and-braces with the extractTarball filter —
145
+ // if a symlink somehow survived (shouldn't), don't auto-follow it
146
+ // into secrets on this hop either.
147
+ const finalDir = join(tmpRoot, 'payload');
148
+ cpSync(payloadRoot, finalDir, { recursive: true, verbatimSymlinks: true });
149
+ return { tmpDir: finalDir, sourceUrl, inferredKind };
150
+ }
151
+ async function fetchNpm(pkg) {
152
+ // Resolve registry metadata to find the tarball URL of the latest
153
+ // dist-tag. Honour `npm:<pkg>@<version>` for pinning.
154
+ let name = pkg;
155
+ let version = 'latest';
156
+ const atIdx = pkg.lastIndexOf('@');
157
+ if (atIdx > 0) {
158
+ // Watch out for scoped packages — leading '@' is the scope marker.
159
+ name = pkg.slice(0, atIdx);
160
+ version = pkg.slice(atIdx + 1);
161
+ }
162
+ const registryBase = process.env.NPM_REGISTRY ?? 'https://registry.npmjs.org';
163
+ const metaUrl = `${registryBase}/${encodeURIComponent(name).replace(/^%40/, '@')}`;
164
+ const meta = await fetchJson(metaUrl, `npm registry ${name}`);
165
+ const distTags = meta['dist-tags'];
166
+ let targetVersion = version;
167
+ if (distTags && typeof distTags === 'object' && version in distTags) {
168
+ targetVersion = distTags[version] ?? version;
169
+ }
170
+ const versions = meta.versions;
171
+ const versionMeta = versions?.[targetVersion];
172
+ const tarballUrl = versionMeta?.dist?.tarball;
173
+ if (!tarballUrl) {
174
+ throw new Error(`SOURCE_NPM: no tarball for ${name}@${targetVersion}`);
175
+ }
176
+ const tmpRoot = mkdtempSync(join(tmpdir(), 'pugi-skill-npm-'));
177
+ const tarPath = join(tmpRoot, 'payload.tgz');
178
+ await downloadToFile(tarballUrl, tarPath, `npm ${name}@${targetVersion}`);
179
+ const extractDir = join(tmpRoot, 'extract');
180
+ mkdirSync(extractDir, { recursive: true });
181
+ await extractTarball(tarPath, extractDir);
182
+ // npm tarballs unpack as `package/` at the root.
183
+ const packageRoot = join(extractDir, 'package');
184
+ if (!existsSync(packageRoot)) {
185
+ throw new Error('SOURCE_NPM: expected "package/" root in npm tarball');
186
+ }
187
+ const inferredKind = inferKind(packageRoot);
188
+ const finalDir = join(tmpRoot, 'payload');
189
+ cpSync(packageRoot, finalDir, { recursive: true, verbatimSymlinks: true });
190
+ return { tmpDir: finalDir, sourceUrl: tarballUrl, inferredKind };
191
+ }
192
+ async function fetchLocal(rawPath) {
193
+ const abs = isAbsolute(rawPath) ? rawPath : resolve(process.cwd(), rawPath);
194
+ if (!existsSync(abs)) {
195
+ throw new Error(`SOURCE_LOCAL: path does not exist: ${abs}`);
196
+ }
197
+ const tmpRoot = mkdtempSync(join(tmpdir(), 'pugi-skill-local-'));
198
+ const finalDir = join(tmpRoot, 'payload');
199
+ const stat = statSync(abs);
200
+ if (stat.isFile()) {
201
+ // Single-file agent install: copy as-is into the tmp dir.
202
+ mkdirSync(finalDir, { recursive: true });
203
+ cpSync(abs, join(finalDir, abs.split(sep).pop() ?? 'agent.md'), { verbatimSymlinks: true });
204
+ }
205
+ else {
206
+ cpSync(abs, finalDir, { recursive: true, verbatimSymlinks: true });
207
+ }
208
+ const inferredKind = inferKind(finalDir);
209
+ return { tmpDir: finalDir, sourceUrl: `file://${abs}`, inferredKind };
210
+ }
211
+ async function fetchCatalog(name) {
212
+ const url = `${CATALOG_BASE}/api/skills/${encodeURIComponent(name)}`;
213
+ let meta = null;
214
+ try {
215
+ meta = await fetchJson(url, `catalog ${name}`);
216
+ }
217
+ catch (error) {
218
+ const message = error instanceof Error ? error.message : String(error);
219
+ throw new Error(`CATALOG_UNREACHABLE: could not query ${CATALOG_BASE} for "${name}" (${message}). Try a direct source like "gh:anthropics/skills/${name}@main".`);
220
+ }
221
+ if (!meta || typeof meta !== 'object') {
222
+ throw new Error(`CATALOG_NOT_FOUND: skill "${name}" not found in ${CATALOG_BASE}. Did you mean "gh:anthropics/skills/${name}@main"?`);
223
+ }
224
+ const upstream = meta.source;
225
+ if (typeof upstream !== 'string') {
226
+ throw new Error(`CATALOG_INVALID: catalog entry for "${name}" missing "source" field`);
227
+ }
228
+ // Catalog payload tells us which canonical source to fetch. Recurse.
229
+ return fetchSource(upstream);
230
+ }
231
+ /**
232
+ * Probe the payload root and decide whether it looks like a skill
233
+ * (`SKILL.md` at root) or an agent (single `.md` at root).
234
+ * Tie-breaker: SKILL.md wins because Skills are the dominant format.
235
+ */
236
+ function inferKind(dir) {
237
+ const entries = readdirSync(dir);
238
+ if (entries.some((name) => name === 'SKILL.md')) {
239
+ return 'skill';
240
+ }
241
+ const mdFiles = entries.filter((name) => name.toLowerCase().endsWith('.md'));
242
+ if (mdFiles.length === 1) {
243
+ return 'agent';
244
+ }
245
+ if (mdFiles.length > 1) {
246
+ // Multiple markdowns without a SKILL.md — assume skill, the loader
247
+ // will throw a clear "missing SKILL.md" error.
248
+ return 'skill';
249
+ }
250
+ return 'skill';
251
+ }
252
+ const MAX_REDIRECTS = 5;
253
+ /**
254
+ * Internal redirect-following GET. undici@8 does not honour
255
+ * `maxRedirections` on the top-level `request` call (it lives on the
256
+ * Agent), so we walk redirects manually. Hop cap prevents loops.
257
+ */
258
+ async function requestFollow(url) {
259
+ let currentUrl = url;
260
+ // SSRF guard: every hop (initial + each redirect target) must resolve
261
+ // to a public address. The redirect-following loop below re-runs the
262
+ // guard on the post-redirect URL so a 302 → http://169.254.169.254/
263
+ // (AWS metadata service) cannot smuggle a private fetch.
264
+ //
265
+ // We reuse the shared `validateHostnameForFetch` from web-fetch.ts so
266
+ // there is one canonical IPv4/IPv6 blocklist + DNS-resolution check
267
+ // across every Pugi outbound surface (web_fetch tool, skills installer,
268
+ // future: webhook delivery). Drift between two copies of that block
269
+ // list would be a real footgun — the SSRF cheat-sheet covers ~10
270
+ // ranges and missing one (e.g. SIIT/NAT64) is exactly the class of
271
+ // bug Codex caught in PR #349.
272
+ // Initial scheme — locked for entire redirect chain. Codex P2 review
273
+ // (PR #362 v2): an HTTPS source that 302s к public http:// URL would
274
+ // otherwise be fetched cleartext, MITM tampers payload. Stay TLS.
275
+ const initialScheme = new URL(currentUrl).protocol;
276
+ await guardOutboundUrl(currentUrl, 'initial request', initialScheme);
277
+ for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
278
+ const response = await request(currentUrl, {
279
+ method: 'GET',
280
+ headersTimeout: FETCH_TIMEOUT_MS,
281
+ bodyTimeout: FETCH_TIMEOUT_MS,
282
+ });
283
+ if (response.statusCode >= 300 && response.statusCode < 400) {
284
+ const loc = response.headers['location'];
285
+ const locStr = Array.isArray(loc) ? loc[0] : loc;
286
+ if (typeof locStr !== 'string' || locStr.length === 0) {
287
+ return response;
288
+ }
289
+ // Drain body so socket reusable.
290
+ await response.body.dump();
291
+ const nextUrl = new URL(locStr, currentUrl).toString();
292
+ await guardOutboundUrl(nextUrl, `redirect from ${currentUrl}`, initialScheme);
293
+ currentUrl = nextUrl;
294
+ continue;
295
+ }
296
+ return response;
297
+ }
298
+ throw new Error(`SOURCE_NETWORK: redirect limit (${MAX_REDIRECTS}) exceeded`);
299
+ }
300
+ // P2 DNS rebinding follow-up: pinned-address Dispatcher с undici lookup
301
+ // hook. Filed task — TOCTOU window microseconds + needs attacker DNS
302
+ // control. Acceptable v1 trade-off; not blocking initial ship.
303
+ /**
304
+ * SSRF gate for one outbound URL hop. Throws `SOURCE_SSRF` when the
305
+ * URL is malformed, uses a non-http(s) scheme, or resolves to any
306
+ * private/loopback/link-local/CGNAT/metadata range.
307
+ *
308
+ * Called from `requestFollow` on the initial URL and every redirect
309
+ * target so a 302 → http://10.0.0.5/ (or → http://169.254.169.254/)
310
+ * cannot bypass the gate. Also rejects scheme downgrades (https → http)
311
+ * so a redirect that takes us off TLS aborts loudly instead of silently.
312
+ */
313
+ async function guardOutboundUrl(rawUrl, label, initialScheme) {
314
+ let parsed;
315
+ try {
316
+ parsed = new URL(rawUrl);
317
+ }
318
+ catch {
319
+ throw new Error(`SOURCE_SSRF: ${label} URL is malformed: ${rawUrl}`);
320
+ }
321
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
322
+ throw new Error(`SOURCE_SSRF: ${label} uses unsupported scheme ${parsed.protocol} (only http/https).`);
323
+ }
324
+ // Codex P2 PR #362 v2: HTTPS source MUST NOT downgrade к HTTP across
325
+ // redirect chain — would let MITM tamper с payload after the initial
326
+ // TLS hop. Once we started TLS, stay TLS.
327
+ if (initialScheme === 'https:' && parsed.protocol === 'http:') {
328
+ throw new Error(`SOURCE_SSRF: ${label} attempts HTTPS→HTTP downgrade — refused (payload integrity required).`);
329
+ }
330
+ const verdict = await validateHostnameForFetch(parsed.hostname);
331
+ if (verdict !== null) {
332
+ throw new Error(`SOURCE_SSRF: ${label} refused — ${verdict}`);
333
+ }
334
+ }
335
+ async function downloadToFile(url, outPath, label) {
336
+ try {
337
+ const response = await requestFollow(url);
338
+ if (response.statusCode < 200 || response.statusCode >= 300) {
339
+ const body = await response.body.text();
340
+ const err = new Error(`SOURCE_NETWORK: ${label} returned HTTP ${response.statusCode}. ${body.slice(0, 200)}`);
341
+ err.status = response.statusCode;
342
+ throw err;
343
+ }
344
+ mkdirSync(dirname(outPath), { recursive: true });
345
+ const chunks = [];
346
+ let total = 0;
347
+ for await (const chunk of response.body) {
348
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
349
+ total += buf.byteLength;
350
+ if (total > MAX_PAYLOAD_BYTES) {
351
+ throw new Error(`SOURCE_SIZE: ${label} payload exceeded ${Math.round(MAX_PAYLOAD_BYTES / (1024 * 1024))}MB cap`);
352
+ }
353
+ chunks.push(buf);
354
+ }
355
+ writeFileSync(outPath, Buffer.concat(chunks));
356
+ }
357
+ catch (error) {
358
+ if (error instanceof Error && error.message.startsWith('SOURCE_')) {
359
+ throw error;
360
+ }
361
+ const message = error instanceof Error ? error.message : String(error);
362
+ throw new Error(`SOURCE_NETWORK: ${label} fetch failed (${message})`);
363
+ }
364
+ }
365
+ async function fetchJson(url, label) {
366
+ const response = await requestFollow(url);
367
+ if (response.statusCode === 404) {
368
+ throw new Error(`SOURCE_NOT_FOUND: ${label} returned HTTP 404`);
369
+ }
370
+ if (response.statusCode < 200 || response.statusCode >= 300) {
371
+ throw new Error(`SOURCE_NETWORK: ${label} returned HTTP ${response.statusCode}`);
372
+ }
373
+ const text = await response.body.text();
374
+ try {
375
+ return JSON.parse(text);
376
+ }
377
+ catch {
378
+ throw new Error(`SOURCE_PARSE: ${label} returned invalid JSON`);
379
+ }
380
+ }
381
+ async function extractTarball(tarPath, destDir) {
382
+ // Use the `tar` package (already on disk via transitive hoisting) so
383
+ // we get streaming gunzip + extraction without a custom parser.
384
+ // Dynamic import keeps the dependency lazy: operators who never
385
+ // install a skill never load tar.
386
+ //
387
+ // Security model: we collect filter violations in `violations`
388
+ // rather than throwing inside the filter callback. node-tar v6
389
+ // dispatches the filter from inside the streaming parser; a sync
390
+ // throw there surfaces as an uncaughtException because the parser's
391
+ // internal event chain is not awaited by tar.x's promise. Skipping
392
+ // (return false) keeps the stream healthy; we abort after extraction
393
+ // completes so no hostile entry is ever materialised to disk AND
394
+ // the operator sees a precise error.
395
+ const tarModule = await loadTarModule();
396
+ const violations = [];
397
+ await tarModule.x({
398
+ file: tarPath,
399
+ cwd: destDir,
400
+ // strict: true rejects bad records (bad checksums, truncated
401
+ // headers, mtime-newer-than-now). Required for defense-in-depth
402
+ // even though our filter below catches the high-value cases.
403
+ strict: true,
404
+ // Filter returns false to skip the entry. We accumulate the
405
+ // violations and throw AFTER extraction completes (see below).
406
+ filter: (path, entry) => {
407
+ // 1. Block any symlink or hardlink — these are the tar-slip
408
+ // vectors. A symlink to ../../home/user/.ssh + a follow-up
409
+ // write to that symlink would exfil secrets.
410
+ if (entry.type === 'SymbolicLink' || entry.type === 'Link') {
411
+ violations.push(`SOURCE_TAR_SYMLINK: tarball contains ${entry.type} entry (${path} → ${entry.linkpath ?? '?'}). Refusing extraction.`);
412
+ return false;
413
+ }
414
+ // 2. Block absolute paths — `tar` strips the leading "/" in
415
+ // permissive mode and writes anyway. We refuse such entries.
416
+ if (path.startsWith('/')) {
417
+ violations.push(`SOURCE_TAR_ABSOLUTE: tarball entry has absolute path: ${path}`);
418
+ return false;
419
+ }
420
+ // 3. Block parent-traversal segments. `..` as a path segment
421
+ // cannot be present in any legitimate skill/agent payload.
422
+ const segments = path.split(/[\\/]+/);
423
+ if (segments.includes('..')) {
424
+ violations.push(`SOURCE_TAR_TRAVERSAL: tarball entry has parent-traversal segment: ${path}`);
425
+ return false;
426
+ }
427
+ // 4. Block null-byte truncation attempts.
428
+ if (path.includes('\0')) {
429
+ violations.push(`SOURCE_TAR_NULLBYTE: tarball entry contains null byte: ${JSON.stringify(path)}`);
430
+ return false;
431
+ }
432
+ return true;
433
+ },
434
+ });
435
+ if (violations.length > 0) {
436
+ // Throw the FIRST violation verbatim so callers can pattern-match
437
+ // on the specific code (SOURCE_TAR_SYMLINK / _ABSOLUTE / _TRAVERSAL
438
+ // / _NULLBYTE). Append a count summary when there are multiple.
439
+ const head = violations[0] ?? 'SOURCE_TAR: unspecified violation';
440
+ if (violations.length === 1)
441
+ throw new Error(head);
442
+ throw new Error(`${head} (and ${violations.length - 1} more refused entries)`);
443
+ }
444
+ }
445
+ /**
446
+ * Lazy-loaded `tar` module reference. Decoupled so tests can stub it.
447
+ */
448
+ let cachedTarModule = null;
449
+ async function loadTarModule() {
450
+ if (cachedTarModule)
451
+ return cachedTarModule;
452
+ // `tar` is a CJS module exporting `x`/`c`/`u`/`t`. We type it loosely
453
+ // because we only need the extract entry-point.
454
+ const imported = (await import('tar'));
455
+ cachedTarModule = imported;
456
+ return imported;
457
+ }
458
+ /**
459
+ * Best-effort tmp cleanup. Never throws — install path must succeed
460
+ * even when the OS refuses to delete a tmp dir (rare but possible on
461
+ * Windows under tests).
462
+ */
463
+ export function cleanupTmp(tmpDir) {
464
+ try {
465
+ // Walk up to the mkdtemp parent: tmpDir was created by either
466
+ // moving into `<root>/payload` or by reading `<root>/payload`. We
467
+ // delete the parent so the tarball + extract dir also go away.
468
+ const parent = dirname(tmpDir);
469
+ if (parent.includes('pugi-skill-')) {
470
+ rmSync(parent, { recursive: true, force: true });
471
+ }
472
+ else {
473
+ rmSync(tmpDir, { recursive: true, force: true });
474
+ }
475
+ }
476
+ catch {
477
+ /* swallow */
478
+ }
479
+ }
480
+ //# sourceMappingURL=sources.js.map
@@ -0,0 +1,172 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync, } from 'node:fs';
3
+ import { homedir } from 'node:os';
4
+ import { dirname, join, resolve } from 'node:path';
5
+ import { z } from 'zod';
6
+ const trustEntrySchema = z.object({
7
+ kind: z.enum(['skill', 'agent']),
8
+ scope: z.enum(['global', 'workspace']),
9
+ name: z.string().min(1),
10
+ sha256: z.string().regex(/^[0-9a-f]{64}$/),
11
+ source: z.string().min(1),
12
+ signedAt: z.string().datetime(),
13
+ signedBy: z.string().min(1),
14
+ });
15
+ const trustRegistrySchema = z.object({
16
+ schema: z.number().int().positive().default(1),
17
+ entries: z.array(trustEntrySchema).default([]),
18
+ });
19
+ const TRUST_REGISTRY_FILENAME = 'trust.json';
20
+ function registryPath() {
21
+ const home = process.env.PUGI_HOME ?? resolve(homedir(), '.pugi');
22
+ return resolve(home, TRUST_REGISTRY_FILENAME);
23
+ }
24
+ function readRegistry() {
25
+ const path = registryPath();
26
+ if (!existsSync(path)) {
27
+ return { schema: 1, entries: [] };
28
+ }
29
+ const raw = readFileSync(path, 'utf8');
30
+ if (raw.trim() === '') {
31
+ return { schema: 1, entries: [] };
32
+ }
33
+ // Recovery path for a corrupt trust.json. Without this, a single
34
+ // malformed entry (truncated write on power loss, partial disk
35
+ // corruption, manual edit gone wrong) would brick every skill +
36
+ // agent surface: every command calls readRegistry. We back up the
37
+ // bad file (preserving forensic evidence) and reset to an empty
38
+ // registry. The operator must re-trust on next install — strictly
39
+ // safer than auto-trusting on-disk payloads.
40
+ let parsedJson;
41
+ try {
42
+ parsedJson = JSON.parse(raw);
43
+ }
44
+ catch (error) {
45
+ const message = error instanceof Error ? error.message : String(error);
46
+ const backup = `${path}.corrupt-${Date.now()}`;
47
+ try {
48
+ renameSync(path, backup);
49
+ }
50
+ catch {
51
+ /* swallow — best-effort backup */
52
+ }
53
+ process.stderr.write(`[pugi] trust.json invalid JSON: ${message}. Backed up to ${backup}. Resetting to empty registry.\n`);
54
+ return { schema: 1, entries: [] };
55
+ }
56
+ try {
57
+ return trustRegistrySchema.parse(parsedJson);
58
+ }
59
+ catch (error) {
60
+ const message = error instanceof Error ? error.message : String(error);
61
+ const backup = `${path}.corrupt-${Date.now()}`;
62
+ try {
63
+ renameSync(path, backup);
64
+ }
65
+ catch {
66
+ /* swallow */
67
+ }
68
+ process.stderr.write(`[pugi] trust.json failed schema validation: ${message}. Backed up to ${backup}. Resetting to empty registry.\n`);
69
+ return { schema: 1, entries: [] };
70
+ }
71
+ }
72
+ function writeRegistry(registry) {
73
+ const path = registryPath();
74
+ mkdirSync(dirname(path), { recursive: true });
75
+ // Atomic write: write to a unique temp file, fsync via writeFileSync's
76
+ // default behaviour, then rename(2) over the live path. POSIX rename
77
+ // is atomic on the same filesystem, so a crash between write+rename
78
+ // leaves trust.json EITHER pre-state OR post-state — never a
79
+ // half-written file that would trip the schema parser on the next
80
+ // read. Mode 0o600 — registry reveals which third-party skills the
81
+ // operator has approved. Parity with the other Pugi trust ledgers.
82
+ const tmp = `${path}.${process.pid}.${Date.now()}.tmp`;
83
+ writeFileSync(tmp, `${JSON.stringify(registry, null, 2)}\n`, {
84
+ encoding: 'utf8',
85
+ mode: 0o600,
86
+ });
87
+ renameSync(tmp, path);
88
+ }
89
+ function entryKey(kind, scope, name) {
90
+ return `${kind}:${scope}:${name}`;
91
+ }
92
+ /**
93
+ * Walk a directory tree and produce a stable sha256 over its contents.
94
+ * Sorting filenames gives reproducible hashes across filesystems with
95
+ * different `readdir` orderings (ext4 vs apfs).
96
+ *
97
+ * Files are hashed as `<relative-path>\0<bytes>\0` segments so a file
98
+ * rename inside the tree is detectable even when total bytes are equal.
99
+ */
100
+ export function hashSkillDir(rootDir) {
101
+ const hasher = createHash('sha256');
102
+ const walk = (dir, prefix) => {
103
+ const names = readdirSync(dir).sort((a, b) => a.localeCompare(b));
104
+ for (const name of names) {
105
+ const full = join(dir, name);
106
+ const rel = prefix ? `${prefix}/${name}` : name;
107
+ const stat = statSync(full);
108
+ if (stat.isDirectory()) {
109
+ walk(full, rel);
110
+ }
111
+ else if (stat.isFile()) {
112
+ hasher.update(rel);
113
+ hasher.update('\0');
114
+ hasher.update(readFileSync(full));
115
+ hasher.update('\0');
116
+ }
117
+ }
118
+ };
119
+ walk(rootDir, '');
120
+ return hasher.digest('hex');
121
+ }
122
+ /**
123
+ * sha256 of a single file — used for agent payloads (single .md file
124
+ * at `~/.pugi/agents/<slug>.md`).
125
+ */
126
+ export function hashAgentFile(filePath) {
127
+ const hasher = createHash('sha256');
128
+ hasher.update(readFileSync(filePath));
129
+ return hasher.digest('hex');
130
+ }
131
+ export async function recordTrust(input) {
132
+ const registry = readRegistry();
133
+ const key = entryKey(input.kind, input.scope, input.name);
134
+ const filtered = registry.entries.filter((entry) => entryKey(entry.kind, entry.scope, entry.name) !== key);
135
+ filtered.push({
136
+ kind: input.kind,
137
+ scope: input.scope,
138
+ name: input.name,
139
+ sha256: input.sha256,
140
+ source: input.source,
141
+ signedAt: new Date().toISOString(),
142
+ signedBy: input.signedBy,
143
+ });
144
+ writeRegistry({ schema: registry.schema, entries: filtered });
145
+ }
146
+ export async function getTrust(kind, scope, name) {
147
+ const registry = readRegistry();
148
+ const key = entryKey(kind, scope, name);
149
+ return (registry.entries.find((entry) => entryKey(entry.kind, entry.scope, entry.name) === key) ??
150
+ null);
151
+ }
152
+ export async function revokeTrust(kind, scope, name) {
153
+ const registry = readRegistry();
154
+ const key = entryKey(kind, scope, name);
155
+ const filtered = registry.entries.filter((entry) => entryKey(entry.kind, entry.scope, entry.name) !== key);
156
+ writeRegistry({ schema: registry.schema, entries: filtered });
157
+ }
158
+ export async function listTrust() {
159
+ const registry = readRegistry();
160
+ return [...registry.entries];
161
+ }
162
+ export async function verifyTrust(kind, scope, name, actualSha256) {
163
+ const entry = await getTrust(kind, scope, name);
164
+ if (!entry) {
165
+ return { status: 'unsigned' };
166
+ }
167
+ if (entry.sha256 !== actualSha256) {
168
+ return { status: 'mismatch', recorded: entry.sha256, actual: actualSha256 };
169
+ }
170
+ return { status: 'trusted', signedAt: entry.signedAt, signedBy: entry.signedBy };
171
+ }
172
+ //# sourceMappingURL=trust.js.map