mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-31 04:07:52 +00:00
* docs(designs): promote skillpack registry v1 spec with v0.36 alignment header Strategic spec produced via /office-hours → /plan-ceo-review → /plan-eng-review → /plan-devex-review (two rounds) → /codex outside-voice. 27 locked decisions: 6 CEO scope, 5 eng architecture, 8 DX (artifact cathedral + rubric/doctor + 10/10 bundled invariant), 8 codex (T1 per-step runbook, T4 required-core+badges, G1 state.json, G2 env scrub, G3 CI workflow split, G4 anti-typosquat, plus tarball determinism / pack-local resolver / api_version ranges). 2 cathedral defenses documented (T2 scope, T3 10/10 invariant) as taste-of-cathedral product calls. Lake Score: 25/27. Spec carries a top-of-file alignment header noting the v0.36.0.0 retirement of the managed-block install model. Verbs and integration points re-map: install → scaffold from third-party source; uninstall → user-owns-files; auto-walk → display bootstrap.md; multi-source receipt → state.json. Strategic decisions (registry + tarball + doctor + rubric + TOFU + sandbox + CI split + anti-typosquat) translate verbatim. Implementation starts in subsequent commits on this branch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(skillpack): foundation layer — manifest validator + tarball + state.json Three pure-data modules every other skillpack-registry feature builds on top of. Each is independently testable; together they form the trust + transport substrate for third-party scaffold. - src/core/skillpack/manifest-v1.ts Third-party skillpack.json runtime validator. Schema is gbrain-skillpack-v1 plus forward-compat runbook_schema_version + eval_schema_version (codex outside-voice). Shape is a superset of bundle.ts's BundleManifest so the existing v0.36 scaffold + reference pipelines (enumerateScaffoldEntries + loadSkillSources) consume third-party packs via bundleManifestFromSkillpack() without any changes. SkillpackManifestError carries a structured code + field so the publish-gate and doctor format actionable messages. - src/core/skillpack/tarball.ts Deterministic pack + allowlist-gated extract. Pack uses GNU tar with --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner --pax-option + GZIP=-n + TZ=UTC so same dir -> same SHA-256 across hosts and clocks. Extract pre-flights every entry: rejects symlinks / hardlinks / devices / FIFOs (allowlist is regular files + dirs only), checks path traversal, enforces caps (maxFiles=5000, maxBytesPerFile=1MB, maxTotalBytes=100MB, maxPathLength=255, maxCompressionRatio=100:1 for bomb defense). Extract prefers GNU tar so --list --verbose output is parser-stable across macOS (bsdtar default) and Linux. Throws TarballError with structured codes. - src/core/skillpack/state.ts Machine-owned trust store at ~/.gbrain/skillpack-state.json. Codex G1 fix: TOFU SHA-256, pinned commits, source URLs, scaffold timestamps live here, NOT in editable markdown. Atomic .tmp + rename write; schema-versioned; immutable upsert/remove for testability. isAlreadyTrusted() encodes the codex G4 first-install-confirm logic (skip prompt only when name + author + pinned_commit-or-tarball-SHA all match — defends author-transfer attacks). Tests: 64 cases across 3 files; all green. Tarball tests skip-gracefully when GNU tar is unavailable (macOS without `brew install gnu-tar`); CI Linux has GNU tar by default. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(skillpack): third-party scaffold — owner/repo, https URL, .tgz, local path End-to-end third-party scaffold pipeline composed from the foundation layer plus three new modules. `gbrain skillpack scaffold <source>` resolves any of: owner/repo (expands to https://github.com/owner/repo.git) https://github.com/.../...git (verbatim https URL, SSRF-checked) /abs/path/to/dir (local pack root) /abs/path/to/pack.tgz (local tarball) Bare kebab names ("book-mirror") keep routing to the v0.36 bundled-skill path; the dispatcher disambiguates on the literal `/` / `://` / `.tgz` shape in the spec. No regression to v0.36 (all 272 existing skillpack tests pass). - src/core/skillpack/remote-source.ts classifySpec() is the pure-fn router. resolveSource() does the I/O: ls-remotes the git HEAD SHA, shallow-clones into ~/.gbrain/skillpack-cache/git/<host>/<owner>/<repo>/<sha>/ on miss, short-circuits on cache hit. Tarballs extract into ~/.gbrain/skillpack-cache/tarball/<sha256>/ and findPackRoot hops one level deep when the tarball wraps its source dir (the packTarball convention). Local paths skip the cache entirely (user owns the dir). Reuses git-remote.ts SSRF guards verbatim; staging dirs prevent partial-clone cache poisoning. - src/core/skillpack/trust-prompt.ts Codex G4 first-install identity confirm. renderIdentityBlock() prints name + version + author + source + pinned commit / tarball SHA + tier + description; askTrust() runs the y/N prompt. isAlreadyTrusted() (in state.ts) drives the skip path — same (name, author, pin/SHA) triple = no prompt. Author mismatch always re-prompts (transfer-attack defense). Local sources skip the gate entirely. - src/core/skillpack/bootstrap-display.ts Codex T1 fix: no executor for install runbooks. buildBootstrapDisplay() reads runbooks/bootstrap.md and returns a framed text block with a loud header making clear gbrain DOES NOT auto-execute the steps — third-party packs run in trusted-path mode and an auto-walker is the npm-postinstall supply-chain hole we explicitly refuse to ship. The agent reads the framed output and walks per-step at its own discretion. - src/core/skillpack/scaffold-third-party.ts Orchestrator. Loads + validates the third-party manifest, checks gbrain_min_version, runs the trust prompt, projects skillpack.json to BundleManifest shape so enumerateScaffoldEntries (v0.36 path) consumes it without changes, runs copyArtifacts (refuses to overwrite the v0.36 way), upserts state.json, returns the framed bootstrap. Pure semver compare for the version gate; no external dep. - src/commands/skillpack.ts dispatch extension cmdScaffold now disambiguates: contains `/` / `://` / `.tgz` → runThirdPartyScaffold. JSON output envelope matches the rest of the v0.36 skillpack surface (ok + status + pack + source + trust + copy summary + bootstrap_shown). New flags: --trust, --no-cache. - src/core/skillpack/tarball.ts typing fix Promote ExtractCaps to a named interface (was inline `as const`) so Partial<ExtractCaps> overrides accept plain numeric literals. Tests: 11 new (scaffold orchestrator) + 18 (remote source) + 12 (trust) + 5 (bootstrap display) = 46 new cases; all green. End-to-end CLI smoke verified: built local pack fixture, `gbrain skillpack scaffold ./pack --workspace ./ws` lands files, refuses overwrite on re-run, writes state.json, displays bootstrap. Typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(skillpack): registry catalog — schema + fetch client + search/info/registry CLI The discovery layer. `garrytan/gbrain-skillpack-registry` will be a separate GitHub repo with two JSON files; this commit teaches gbrain to read them. - src/core/skillpack/registry-schema.ts Runtime validators for registry.json (gbrain-registry-v1) and endorsements.json (gbrain-endorsements-v1). Codex G3 separation: catalog entries land via PR with default_tier = community / experimental / dead; endorsements.json is Garry-only and OVERLAYS tier at read time. effectiveTier() resolves the overlay. RegistrySchemaError carries structured code + field path so the publish-gate formats actionable rejection messages. - src/core/skillpack/registry-client.ts Network fetch + cache + stale-fallback. Default URLs point at garrytan/gbrain-skillpack-registry; overridable via config key skillpack.registry_url or --url. Cache lives at ~/.gbrain/skillpack-cache/registry-<sha16>.json with a 1h soft TTL (cache_warm) before triggering fetch, escalating to "cache > 7d" warning (cache_hard_stale) when offline. Hard-fail only when no cache AND no network (no_cache_no_network). Etag-aware: 304 responses refresh the cache timestamp without re-downloading. findPack / findPackWithTier / searchPacks are pure functions over the loaded catalog; search sorts by tier (endorsed > community > experimental > dead) then alphabetical. - src/commands/skillpack.ts — three new subcommands + kebab-→-registry wiring gbrain skillpack search [<query>] [--tier T] [--refresh] [--url URL] [--json] gbrain skillpack info <name> [--refresh] [--url URL] [--json] gbrain skillpack registry [--url URL] [--refresh] [--json] cmdScaffold now disambiguates kebab inputs: bundled-skill slug first (existing v0.36 path), then registry lookup. `gbrain skillpack scaffold hackathon-evaluation` works once the catalog ships. - src/core/skillpack/trust-prompt.ts + state.ts Extend SkillpackTier with 'dead' so the catalog's tombstone tier flows through the trust-prompt + state-recording paths. Tests: 21 (registry-schema) + 19 (registry-client) = 40 new cases; all green across 312 skillpack-related tests. End-to-end CLI smoke: served fixture registry.json over localhost HTTP, ran `skillpack registry`, `search`, `search founder`, `info hackathon-evaluation` — all return correct output with endorsement overlay applied. Typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(skillpack): rubric + doctor + audit — 10-dimension quality bar The quality bar makes the registry meaningful. Codex T4: rubric splits into REQUIRED CORE (5 dims that gate publish) + QUALITY BADGES (5 dims that gate tier eligibility). A pack with 0 badges still publishes as experimental; community needs >=3 badges; endorsed needs all 5. - src/core/skillpack/rubric.ts Declarative SKILLPACK_RUBRIC_V1 — 10 binary dimensions, single source of truth for doctor + (future) anatomy doc generator. CORE (5): 1. manifest_valid — skillpack.json passes v1 schema 2. skills_have_skill_md — every skill has SKILL.md w/ valid frontmatter 3. routing_evals_present — every skill has routing-eval.jsonl >= 5 intents 4. skills_have_unique_triggers — MECE at the pack level (codex outside-voice adaptation: v0.36 retired resolver files so the pack-local check shifts from check-resolvable to frontmatter-trigger uniqueness across the pack's own skills) 5. changelog_present_and_current — CHANGELOG.md has entry for manifest.version BADGES (5): 6. unit_tests_present — manifest.unit_tests glob matches >=1 file 7. e2e_tests_present — manifest.e2e_tests glob matches >=1 file 8. llm_eval_present — *.judge.json with cases.length >= 3 9. bootstrap_runbook_present — runbooks/bootstrap.md non-empty (codex T1: v0.36 retired install/uninstall runbooks; bootstrap is the single post-scaffold display) 10. license_present — LICENSE / LICENSE.md / LICENSE.txt non-empty walkRubric() is async (each dim's check returns a Promise) so a future --full mode can run heavyweight checks inline. describeRubric() returns the pure-data view for the anatomy doc generator. - src/core/skillpack/doctor.ts runDoctor() walks the rubric, returns a structured DoctorResult with schema_version="skillpack-doctor-v1" for stable JSON shape across versions. formatDoctorResult() renders the human view (per-dim pass/fail markers, paste-ready fix hints, tier eligibility, promotion blockers, [auto-fixable] tags). --quick is the only mode in v1; --full prints a follow-up hint pointing at the publish-gate command that lands in a later wave. --fix path applies auto-scaffolds for `auto_fixable: true` dimensions: routing-eval.jsonl stubs (5 example intents per skill), CHANGELOG.md with the current version's date entry, test/example.test.ts stub, e2e/example.e2e.test.ts stub, evals/example.judge.json with 3 stub cases, runbooks/bootstrap.md stub, LICENSE stub. Codex outside-voice mtime guard preserved: refuses to overwrite files whose mtime is newer than skillpack.json's. Requires --yes for unattended runs. - src/core/skillpack/audit.ts ~/.gbrain/audit/skillpack-YYYY-Www.jsonl (ISO-week rotated, mirrors audit-slug-fallback + rerank-audit patterns). logSkillpackEvent is best-effort: stderr warn on failure, never throws. doctor_run + scaffold + search + registry_refresh events recorded for the future `gbrain doctor` skillpack_activity surface (lands with v0.37 doctor-integration wave). - src/commands/skillpack.ts — `doctor` subcommand gbrain skillpack doctor <pack-dir> [--quick|--full] [--fix] [--yes] [--json] Exit codes: 0 if score=10, 1 if 6-9, 2 if blocked/<5. Tests: 21 new cases covering 10/10 fixture, each individual dimension failing in isolation, all four tier eligibility branches, --fix auto-scaffold (with + without --yes), formatDoctorResult shape, describeRubric pure-data, JSONL audit append + read. 333/333 skillpack tests across 23 files. CLI smoke verified. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(skillpack): publisher side — init + pack + 10/10 reference pack The publisher trinity: scaffold a new pack, gate it through the doctor, emit a deterministic tarball. Plus the canonical 10/10 reference pack that lives in this repo as both an example and a CI regression fixture. - src/core/skillpack/init-scaffold.ts `gbrain skillpack init <name>` lands the cathedral tree out of the box: skillpack.json + skills/<name>/SKILL.md + routing-eval.jsonl (5 example intents) + runbooks/bootstrap.md + CHANGELOG.md + README + LICENSE + .gitignore + test/ + e2e/ + evals/<name>.judge.json. A freshly init'd pack scores 10/10 on doctor --quick immediately; publisher edits to make it real. --minimal flag drops test/e2e/evals for power users opting out. Refuses to overwrite any existing file (same contract as v0.36 scaffold). - src/core/skillpack/pack-publish.ts `gbrain skillpack pack [<pack-dir>]` orchestrates: runDoctor(--quick) + refuse if tier_eligibility=blocked + packTarball into <out>/<name>-<version>.tgz with deterministic SHA-256. --dry-run validates only. --skip-doctor is the publish-gate skill's escape hatch (gate runs server-side instead). Both paths log into the skillpack audit JSONL. - src/commands/skillpack.ts — `init` + `pack` subcommands wired HELP_TOP updated to surface search/info/registry/doctor/init/pack alongside the v0.36 commands. - examples/skillpack-reference/ Real 10/10 pack tree shipped inside gbrain's repo. Doubles as an integration-test fixture and a publisher reference. The SKILL.md body actually teaches the third-party contract (frontmatter shape, doctor dimensions, tier eligibility, publisher workflow). README.md walks the tree. - test/skillpack-reference-pack-is-ten.test.ts Regression guard pinning examples/skillpack-reference/ at 10/10 forever. If a future change drops the reference pack below the bar, this test fails with a paste-ready list of regressed dimensions. Per the locked DX-Round-2 invariant: gbrain ships its own bar or doesn't ship it. Tests: 12 (init + pack-publish, including 1 full e2e init->doctor-> pack loop) + 2 (reference pack 10/10 regression) = 14 new cases; 347/347 skillpack-related tests green across 25 files. Typecheck clean. End-to-end CLI smoke: `gbrain skillpack init test-pack` followed by `gbrain skillpack doctor test-pack --quick` followed by `gbrain skillpack pack test-pack` produces a 10/10 verdict and a content-addressable tarball. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(skillpack): anatomy doc generator + e2e third-party flow test Closes the cathedral with the canonical one-page reference doc + the end-to-end test that exercises the full publisher + consumer loop via the actual `gbrain` CLI subprocess. - scripts/build-skillpack-anatomy.ts Regenerates docs/skillpack-anatomy.md between BEGIN/END markers from src/core/skillpack/rubric.ts. Auto-section is the rubric table (core dims + badges); hand-written intro covers the tree map, the agent-uses-pack contract, and the publisher CLI workflow. `--check` flag fails the build when committed doc drifts from rubric.ts — wireable into `bun run verify` later. - docs/skillpack-anatomy.md Initial generated output. 112 lines. Tree diagram + rubric tables + tier eligibility matrix + CLI reference + cross-links to the reference pack and the spec. - test/e2e/skillpack-third-party.test.ts Subprocess-spawning E2E (no in-process imports of CLI internals). Covers: - Full publisher loop: init -> doctor (10/10) -> pack (deterministic SHA) - Full consumer loop: scaffold from local-path -> files land, state.json records, refuse-to-overwrite on re-run - Doctor --fix loop: delete required artifacts -> doctor surfaces gaps -> --fix --yes auto-restores - --minimal init scores 7/10 (3 missing badges that need manifest patches; documents the expected behavior) The localhost-registry search test is skipped: Bun.serve + spawnSync has timing flakiness against bun:test's 5s per-test budget (subprocess startup + fetch round-trip overruns). Network path is fully covered at unit level via the fetchImpl injection seam in test/skillpack-registry-client.test.ts. 369 unit + 5 E2E pass across 27 skillpack test files; 1 intentional skip; 0 fail. Typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(skillpack): route audit-test env mutations through withEnv() helper scripts/check-test-isolation.sh flagged test/skillpack-rubric-doctor.test.ts for direct process.env.GBRAIN_AUDIT_DIR assignment in a beforeEach block — violates rule R1 (env mutations cause cross-file flakiness in the parallel shard runner). Refactored the audit describe block to wrap each test body in `await withEnv({ GBRAIN_AUDIT_DIR: auditDir }, () => { ... })` from test/helpers/with-env.ts. Same semantics, save+restore via try/finally, no contamination of sibling shards. bun run verify now passes the full gate (typecheck + 14 check scripts including check:test-isolation). Sharded test suite via `bun run test`: 7488 pass / 0 fail / 0 skip across 8 shards + 19 serial files. Skillpack slice: 369 unit + 5 E2E pass / 1 intentional skip / 0 fail. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(e2e): update cycle phase-order assertions for v0.36.1.0 hindsight wave Pre-existing master bug surfaced during the skillpack-registry-cathedral E2E run: v0.36.1.0 shipped 3 new cycle phases (propose_takes, grade_takes, calibration_profile) but two E2E tests' expectations were never updated. - test/e2e/dream-cycle-phase-order-pglite.test.ts EXPECTED_PHASES now includes the v0.36.1.0 trio. The first sub-test (`ALL_PHASES matches the documented sequence`) now passes. - test/e2e/cycle.test.ts Phase count assertion bumped 13 -> 16. Comment block extended with the v0.36.1.0 entry in the same shape as the prior version markers. Both files were drift-against-source: cycle.ts (master) lists 16 phases in ALL_PHASES; these tests still asserted 13 from the v0.33.3 baseline. This is a tangential cleanup from the skillpack-registry-cathedral branch — orthogonal to the registry work but caught during the final E2E sweep. A second sub-test in dream-cycle-phase-order-pglite (the dry-run full cycle path) still fails on a runtime SyntaxError from propose_takes importing a non-existent embedMultimodal export from src/core/embedding.ts. That's a separate v0.36.1.0 implementation bug that warrants its own PR; not in scope here. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(e2e): include v0.36.1.0 embedding exports in dream-cycle mock Bun's module linker fails fast when a downstream consumer imports a symbol the mock didn't declare. v0.36.1.0 added embedMultimodal + embedQuery + getEmbeddingModelName + getEmbeddingDimensions to src/core/embedding.ts; the propose_takes phase and other v0.36 phases pull from them, so the mock has to keep parity. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(skillpack): endorse CLI — Garry-only registry tier overlay gbrain skillpack endorse <name> [--tier endorsed|community|experimental|dead] [--note STR] [--push] [--dry-run] Runs inside a clone of garrytan/gbrain-skillpack-registry. Validates that <name> is in registry.json's catalog, mutates endorsements.json through pure applyEndorsement(), stable-key-orders the write, stages, and creates a one-line commit `endorse: <name> -> <tier>`. Optionally pushes to origin. EndorseError surfaces a tagged code (not_a_registry_repo, pack_not_in_catalog, git_commit_failed, git_push_failed) so callers can branch on the failure mode without string parsing. 10 unit + integration cases pinning applyEndorsement immutability, full-flow commits against a real git repo, --dry-run no-write contract, stable key ordering across alpha/zeta inserts, and tier downgrades to dead. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(release): bump to 0.37.0.0 — skillpack registry cathedral Third-party skillpack ecosystem layered on the v0.36 scaffolding contract: manifest-v1 + deterministic tarball + TOFU state.json + SSRF-hardened source resolver + registry catalog client + 10-dim rubric (5 core + 5 badges, codex T4 stub-spam mitigation) + doctor with --fix autoscaffold + init cathedral + pack publisher + Garry-only endorse CLI + JSONL audit + reference pack + auto-generated anatomy doc. Wave includes the prior commits in this branch: - fix(skillpack): route audit-test env mutations through withEnv() - feat(skillpack): rubric + doctor + audit - feat(skillpack): publisher side — init + pack + 10/10 reference - feat(skillpack): anatomy doc generator + e2e third-party flow - test(e2e): update cycle phase-order assertions for v0.36.1.0 - test(e2e): include v0.36.1.0 embedding exports in dream-cycle mock - feat(skillpack): endorse CLI Deferred to follow-ups: subprocess sandbox for publish-gate, garrytan/gbrain-skillpack-registry repo creation + CI workflow split (codex G3), Printing Press cross-list, generated gbrain-cli, W4.5 retrofit of bundled skills to 10/10. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(release): rebump 0.37.0.0 → 0.38.0.0 User requested v0.38.0 (4-segment: 0.38.0.0) as the slot for the skillpack registry cathedral. Pure rename — no scope change, no behavior change. VERSION + package.json + CHANGELOG header + CHANGELOG "To take advantage" section + CLAUDE.md Key Files entry rewritten in lockstep. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore(release): rebump 0.38.0.0 → 0.37.0.0 User picked v0.37.0.0 as the slot for the skillpack registry cathedral (reverts the earlier 0.37 → 0.38 rebump). Master tip is v0.36.6.0, so 0.37.0.0 remains semver-clean. Pure rename — no scope change, no behavior change. VERSION + package.json + CHANGELOG header + "To take advantage" section + lead-paragraph "v0.38" references + CLAUDE.md Key Files annotation all rewritten in lockstep. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
422 lines
13 KiB
TypeScript
422 lines
13 KiB
TypeScript
/**
|
|
* skillpack/remote-source.ts — third-party skillpack source resolution.
|
|
*
|
|
* `resolveSource(spec)` accepts every supported input shape and returns a
|
|
* `ResolvedSource` with the local pack path plus identity metadata the
|
|
* scaffold orchestrator needs (TOFU pin, source URL, kind). Cache layout:
|
|
*
|
|
* ~/.gbrain/skillpack-cache/git/<host>/<owner>/<repo>/<sha>/ (git sources)
|
|
* ~/.gbrain/skillpack-cache/tarball/<sha256-hex>/ (tarball sources)
|
|
*
|
|
* Cache hits short-circuit the clone/extract step. Cache misses do the work
|
|
* and then atomically rename the staging dir into place so partial clones
|
|
* never poison subsequent lookups.
|
|
*
|
|
* Reuses SSRF-hardened `cloneRepo` from `git-remote.ts`. Local-path inputs
|
|
* skip the cache entirely (the user owns the directory).
|
|
*
|
|
* Bare-name inputs ("hackathon-evaluation") are not handled here — the
|
|
* registry-client resolves them to a URL first, then re-invokes
|
|
* `resolveSource` with the URL. Keeps this module independent of the
|
|
* registry layer.
|
|
*/
|
|
|
|
import { execFileSync } from 'child_process';
|
|
import { existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync } from 'fs';
|
|
import { isAbsolute, join, resolve } from 'path';
|
|
|
|
import { gbrainPath } from '../config.ts';
|
|
import { GIT_SSRF_FLAGS, RemoteUrlError, cloneRepo, parseRemoteUrl } from '../git-remote.ts';
|
|
import { extractTarball, fileSha256 } from './tarball.ts';
|
|
|
|
/** Kinds of third-party source we accept. */
|
|
export type ResolvedSourceKind = 'git' | 'tarball' | 'local';
|
|
|
|
export interface ResolvedSource {
|
|
/** Absolute path to the pack root (where skillpack.json lives). */
|
|
path: string;
|
|
/** Source classification. */
|
|
kind: ResolvedSourceKind;
|
|
/** Canonical source URL or path the user provided (after kebab expansion). */
|
|
source: string;
|
|
/** Resolved git commit SHA when kind=git; null otherwise. */
|
|
pinned_commit: string | null;
|
|
/** SHA-256 of the tarball when kind=tarball; null otherwise. */
|
|
tarball_sha256: string | null;
|
|
/** Whether the result came from cache (used by callers for log lines). */
|
|
cache_hit: boolean;
|
|
}
|
|
|
|
export type RemoteSourceErrorCode =
|
|
| 'spec_empty'
|
|
| 'spec_local_missing'
|
|
| 'spec_local_not_pack_root'
|
|
| 'spec_tarball_missing'
|
|
| 'spec_kebab_invalid_shape'
|
|
| 'spec_url_invalid'
|
|
| 'clone_failed'
|
|
| 'rev_parse_failed';
|
|
|
|
export class RemoteSourceError extends Error {
|
|
constructor(
|
|
message: string,
|
|
public code: RemoteSourceErrorCode,
|
|
public detail?: { spec?: string; cause?: string },
|
|
) {
|
|
super(message);
|
|
this.name = 'RemoteSourceError';
|
|
}
|
|
}
|
|
|
|
export interface ResolveSourceOptions {
|
|
/** Override the cache root (test-only; defaults to ~/.gbrain/skillpack-cache). */
|
|
cacheRoot?: string;
|
|
/** Force a fresh clone/extract even when the cache has a hit. */
|
|
noCache?: boolean;
|
|
}
|
|
|
|
/** Result of classifying a raw spec string — narrower than ResolvedSourceKind
|
|
* because `git-url` and `kebab` are pre-resolution states, while `git` is
|
|
* the post-resolution kind on ResolvedSource.
|
|
*/
|
|
export type SpecKind = 'git-url' | 'tarball' | 'local' | 'kebab';
|
|
|
|
/** Classify the spec without doing any I/O beyond a single stat. */
|
|
export function classifySpec(spec: string): { kind: SpecKind; normalized: string } {
|
|
if (!spec || typeof spec !== 'string') {
|
|
throw new RemoteSourceError('source spec is empty', 'spec_empty', { spec });
|
|
}
|
|
const trimmed = spec.trim();
|
|
if (trimmed.length === 0) {
|
|
throw new RemoteSourceError('source spec is empty', 'spec_empty', { spec });
|
|
}
|
|
|
|
// URL: starts with http(s)://, ends in .git or contains github.com / gitlab.com path.
|
|
if (/^https?:\/\//.test(trimmed)) {
|
|
return { kind: 'git-url', normalized: trimmed };
|
|
}
|
|
|
|
// Local path: starts with /, ./, ../, or ~/
|
|
if (
|
|
isAbsolute(trimmed) ||
|
|
trimmed.startsWith('./') ||
|
|
trimmed.startsWith('../') ||
|
|
trimmed.startsWith('~/')
|
|
) {
|
|
const expanded = trimmed.startsWith('~/') ? join(process.env.HOME ?? '', trimmed.slice(2)) : trimmed;
|
|
const abs = resolve(expanded);
|
|
// Tarball if it's a .tgz / .tar.gz file.
|
|
if (/\.(tgz|tar\.gz)$/.test(abs)) {
|
|
return { kind: 'tarball', normalized: abs };
|
|
}
|
|
return { kind: 'local', normalized: abs };
|
|
}
|
|
|
|
// owner/repo short-form (github inferred): exactly one `/` and both halves
|
|
// look like GitHub identifiers (alnum + dash/dot/underscore).
|
|
if (/^[A-Za-z0-9][A-Za-z0-9._-]{0,38}\/[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/.test(trimmed)) {
|
|
return { kind: 'git-url', normalized: `https://github.com/${trimmed}.git` };
|
|
}
|
|
|
|
// Bare kebab-name: defer to registry resolution. Caller (registry-client)
|
|
// must convert to a URL before re-calling resolveSource.
|
|
if (/^[a-z][a-z0-9-]{1,63}$/.test(trimmed)) {
|
|
return { kind: 'kebab', normalized: trimmed };
|
|
}
|
|
|
|
throw new RemoteSourceError(
|
|
`cannot classify source spec ${JSON.stringify(spec)} — must be a kebab name, owner/repo, https URL, local dir, or .tgz path`,
|
|
'spec_kebab_invalid_shape',
|
|
{ spec },
|
|
);
|
|
}
|
|
|
|
/** Compute the cache root, honoring opts override. */
|
|
function cacheRoot(opts: ResolveSourceOptions): string {
|
|
return opts.cacheRoot ?? gbrainPath('skillpack-cache');
|
|
}
|
|
|
|
/** Resolve HEAD SHA of a remote git URL via `git ls-remote`. */
|
|
function resolveRemoteHead(url: string, branch: string | undefined): string {
|
|
const argv = [
|
|
...GIT_SSRF_FLAGS,
|
|
'ls-remote',
|
|
'--exit-code',
|
|
url,
|
|
branch ? `refs/heads/${branch}` : 'HEAD',
|
|
];
|
|
try {
|
|
const output = execFileSync('git', argv, {
|
|
encoding: 'utf-8',
|
|
env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
|
|
timeout: 60_000,
|
|
});
|
|
const firstLine = output.split('\n').find((l) => l.trim().length > 0);
|
|
if (!firstLine) {
|
|
throw new RemoteSourceError(`git ls-remote returned no refs for ${url}`, 'rev_parse_failed', { spec: url });
|
|
}
|
|
const sha = firstLine.split(/\s+/)[0]?.trim();
|
|
if (!sha || !/^[a-f0-9]{40}$/.test(sha)) {
|
|
throw new RemoteSourceError(`git ls-remote gave invalid sha for ${url}: ${firstLine}`, 'rev_parse_failed', { spec: url });
|
|
}
|
|
return sha;
|
|
} catch (err) {
|
|
if (err instanceof RemoteSourceError) throw err;
|
|
throw new RemoteSourceError(
|
|
`git ls-remote failed for ${url}: ${(err as Error).message}`,
|
|
'rev_parse_failed',
|
|
{ spec: url, cause: (err as Error).message },
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Compute the per-source cache directory. */
|
|
function gitCachePath(root: string, parsedUrl: URL, sha: string): string {
|
|
const host = parsedUrl.hostname;
|
|
const ownerRepo = parsedUrl.pathname.replace(/^\/+/, '').replace(/\.git$/, '');
|
|
return join(root, 'git', host, ownerRepo, sha);
|
|
}
|
|
|
|
/** Resolve a git URL into a ResolvedSource. */
|
|
function resolveGitSource(
|
|
url: string,
|
|
opts: ResolveSourceOptions,
|
|
): ResolvedSource {
|
|
let parsedSafe: URL;
|
|
try {
|
|
parsedSafe = new URL(parseRemoteUrl(url).url);
|
|
} catch (err) {
|
|
if (err instanceof RemoteUrlError) {
|
|
throw new RemoteSourceError(
|
|
`remote URL rejected: ${err.message}`,
|
|
'spec_url_invalid',
|
|
{ spec: url, cause: err.code },
|
|
);
|
|
}
|
|
throw err;
|
|
}
|
|
|
|
const sha = resolveRemoteHead(parsedSafe.toString(), undefined);
|
|
const cacheDir = gitCachePath(cacheRoot(opts), parsedSafe, sha);
|
|
|
|
if (!opts.noCache && existsSync(cacheDir)) {
|
|
const entries = readdirSync(cacheDir);
|
|
if (entries.length > 0) {
|
|
return {
|
|
path: cacheDir,
|
|
kind: 'git',
|
|
source: parsedSafe.toString(),
|
|
pinned_commit: sha,
|
|
tarball_sha256: null,
|
|
cache_hit: true,
|
|
};
|
|
}
|
|
}
|
|
|
|
// Stage in a sibling .tmp dir so a failed clone doesn't poison the cache slot.
|
|
const stageDir = cacheDir + '.tmp-' + process.pid + '-' + Date.now();
|
|
mkdirSync(stageDir, { recursive: true });
|
|
try {
|
|
cloneRepo(parsedSafe.toString(), stageDir, { depth: 1, timeoutMs: 600_000 });
|
|
} catch (err) {
|
|
try {
|
|
rmSync(stageDir, { recursive: true, force: true });
|
|
} catch {}
|
|
throw new RemoteSourceError(
|
|
`clone failed for ${parsedSafe.toString()}: ${(err as Error).message}`,
|
|
'clone_failed',
|
|
{ spec: url, cause: (err as Error).message },
|
|
);
|
|
}
|
|
|
|
// Verify the cloned commit matches the SHA we ls-remoted (defense against
|
|
// race where HEAD moved between ls-remote and clone).
|
|
let cloneSha: string;
|
|
try {
|
|
cloneSha = execFileSync('git', ['rev-parse', 'HEAD'], {
|
|
cwd: stageDir,
|
|
encoding: 'utf-8',
|
|
timeout: 30_000,
|
|
}).trim();
|
|
} catch (err) {
|
|
try {
|
|
rmSync(stageDir, { recursive: true, force: true });
|
|
} catch {}
|
|
throw new RemoteSourceError(
|
|
`git rev-parse HEAD failed after clone: ${(err as Error).message}`,
|
|
'rev_parse_failed',
|
|
{ spec: url, cause: (err as Error).message },
|
|
);
|
|
}
|
|
|
|
// Atomic rename into the canonical cache slot. If the slot exists already
|
|
// (cache populated concurrently by another process), prefer the existing
|
|
// one and drop our stage dir.
|
|
if (existsSync(cacheDir)) {
|
|
try {
|
|
rmSync(stageDir, { recursive: true, force: true });
|
|
} catch {}
|
|
} else {
|
|
mkdirSync(join(cacheDir, '..'), { recursive: true });
|
|
renameSync(stageDir, cacheDir);
|
|
}
|
|
|
|
return {
|
|
path: cacheDir,
|
|
kind: 'git',
|
|
source: parsedSafe.toString(),
|
|
pinned_commit: cloneSha,
|
|
tarball_sha256: null,
|
|
cache_hit: false,
|
|
};
|
|
}
|
|
|
|
/** Resolve a local tarball into a ResolvedSource (extract into cache by SHA). */
|
|
function resolveTarballSource(
|
|
tarballPath: string,
|
|
opts: ResolveSourceOptions,
|
|
): ResolvedSource {
|
|
if (!existsSync(tarballPath)) {
|
|
throw new RemoteSourceError(
|
|
`tarball file does not exist: ${tarballPath}`,
|
|
'spec_tarball_missing',
|
|
{ spec: tarballPath },
|
|
);
|
|
}
|
|
if (!statSync(tarballPath).isFile()) {
|
|
throw new RemoteSourceError(
|
|
`tarball path is not a regular file: ${tarballPath}`,
|
|
'spec_tarball_missing',
|
|
{ spec: tarballPath },
|
|
);
|
|
}
|
|
|
|
const sha = fileSha256(tarballPath);
|
|
const cacheDir = join(cacheRoot(opts), 'tarball', sha);
|
|
|
|
if (!opts.noCache && existsSync(cacheDir)) {
|
|
const entries = readdirSync(cacheDir);
|
|
if (entries.length > 0) {
|
|
return findPackRoot({
|
|
path: cacheDir,
|
|
kind: 'tarball',
|
|
source: tarballPath,
|
|
pinned_commit: null,
|
|
tarball_sha256: sha,
|
|
cache_hit: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
const stageDir = cacheDir + '.tmp-' + process.pid + '-' + Date.now();
|
|
mkdirSync(stageDir, { recursive: true });
|
|
try {
|
|
extractTarball({ tgzPath: tarballPath, destDir: stageDir });
|
|
} catch (err) {
|
|
try {
|
|
rmSync(stageDir, { recursive: true, force: true });
|
|
} catch {}
|
|
throw err;
|
|
}
|
|
|
|
if (existsSync(cacheDir)) {
|
|
try {
|
|
rmSync(stageDir, { recursive: true, force: true });
|
|
} catch {}
|
|
} else {
|
|
mkdirSync(join(cacheDir, '..'), { recursive: true });
|
|
renameSync(stageDir, cacheDir);
|
|
}
|
|
|
|
return findPackRoot({
|
|
path: cacheDir,
|
|
kind: 'tarball',
|
|
source: tarballPath,
|
|
pinned_commit: null,
|
|
tarball_sha256: sha,
|
|
cache_hit: false,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* A tarball produced by `packTarball` wraps its source dir, so the extracted
|
|
* cache dir contains `<sourceLeaf>/skillpack.json`, not `skillpack.json` at
|
|
* the root. Find the actual pack root (the directory containing skillpack.json).
|
|
*/
|
|
function findPackRoot(s: ResolvedSource): ResolvedSource {
|
|
if (existsSync(join(s.path, 'skillpack.json'))) return s;
|
|
// Look one level deep.
|
|
const entries = readdirSync(s.path);
|
|
for (const e of entries) {
|
|
const candidate = join(s.path, e);
|
|
if (statSync(candidate).isDirectory() && existsSync(join(candidate, 'skillpack.json'))) {
|
|
return { ...s, path: candidate };
|
|
}
|
|
}
|
|
// Caller will surface a clearer error at manifest-load time; return as-is.
|
|
return s;
|
|
}
|
|
|
|
/** Resolve a local directory: just validate it has skillpack.json. */
|
|
function resolveLocalSource(absPath: string): ResolvedSource {
|
|
if (!existsSync(absPath)) {
|
|
throw new RemoteSourceError(
|
|
`local path does not exist: ${absPath}`,
|
|
'spec_local_missing',
|
|
{ spec: absPath },
|
|
);
|
|
}
|
|
if (!statSync(absPath).isDirectory()) {
|
|
throw new RemoteSourceError(
|
|
`local path is not a directory: ${absPath}`,
|
|
'spec_local_not_pack_root',
|
|
{ spec: absPath },
|
|
);
|
|
}
|
|
if (!existsSync(join(absPath, 'skillpack.json'))) {
|
|
throw new RemoteSourceError(
|
|
`local path is not a pack root (no skillpack.json): ${absPath}`,
|
|
'spec_local_not_pack_root',
|
|
{ spec: absPath },
|
|
);
|
|
}
|
|
return {
|
|
path: absPath,
|
|
kind: 'local',
|
|
source: absPath,
|
|
pinned_commit: null,
|
|
tarball_sha256: null,
|
|
cache_hit: false,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Resolve any supported source spec. Throws RemoteSourceError for kebab-name
|
|
* inputs — those must be resolved through the registry-client first.
|
|
*/
|
|
export function resolveSource(spec: string, opts: ResolveSourceOptions = {}): ResolvedSource {
|
|
const classified = classifySpec(spec);
|
|
switch (classified.kind) {
|
|
case 'git-url':
|
|
return resolveGitSource(classified.normalized, opts);
|
|
case 'tarball':
|
|
return resolveTarballSource(classified.normalized, opts);
|
|
case 'local':
|
|
return resolveLocalSource(classified.normalized);
|
|
case 'kebab':
|
|
throw new RemoteSourceError(
|
|
`bare short-name ${JSON.stringify(classified.normalized)} requires registry lookup — call the registry client first`,
|
|
'spec_kebab_invalid_shape',
|
|
{ spec },
|
|
);
|
|
default: {
|
|
const _exhaustive: never = classified.kind;
|
|
throw new RemoteSourceError(
|
|
`unhandled spec kind: ${String(_exhaustive)}`,
|
|
'spec_kebab_invalid_shape',
|
|
{ spec },
|
|
);
|
|
}
|
|
}
|
|
}
|