@michaelfromyeg/loom-core 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Michael DeMarco
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,449 @@
1
+ import { Plugin, Marketplace, ParseResult, Target, ComponentKind, Component, Badge, ArtifactRecord, Scope, Lockfile, Dependency } from '@michaelfromyeg/loom-schema';
2
+ import { HarnessAdapter, CompiledArtifact, ResolvedMarketplace, ArtifactKind } from '@michaelfromyeg/loom-adapter-kit';
3
+ import { KeyObject } from 'node:crypto';
4
+
5
+ type Severity = "error" | "warning" | "info";
6
+ interface Diagnostic {
7
+ severity: Severity;
8
+ /** Path-precise location: "components[1].mcp", "owner.namespace", or a file path. */
9
+ where: string;
10
+ message: string;
11
+ }
12
+ /** Accumulates diagnostics during compile; fails closed when any error is present. */
13
+ declare class Diagnostics {
14
+ readonly items: Diagnostic[];
15
+ error(where: string, message: string): void;
16
+ warn(where: string, message: string): void;
17
+ info(where: string, message: string): void;
18
+ get hasErrors(): boolean;
19
+ get errors(): Diagnostic[];
20
+ }
21
+ declare class CompileError extends Error {
22
+ readonly diagnostics: Diagnostic[];
23
+ constructor(message: string, diagnostics: Diagnostic[]);
24
+ }
25
+
26
+ /** A plugin whose files are available on disk under `root`. */
27
+ interface FetchedPlugin {
28
+ plugin: Plugin;
29
+ root: string;
30
+ manifestPath: string;
31
+ /** Read a file from the plugin, relative to `root`. */
32
+ read(relPath: string): Buffer;
33
+ /** Recursively list files under a plugin dir, as paths relative to `root` (sorted). */
34
+ list(relDir: string): string[];
35
+ }
36
+ /** A loaded marketplace manifest and its directory. */
37
+ interface FetchedMarketplace {
38
+ marketplace: Marketplace;
39
+ root: string;
40
+ manifestPath: string;
41
+ }
42
+ /** Build `read`/`list` accessors rooted at a directory (used for merged plugins too). */
43
+ declare function fileAccessors(root: string): Pick<FetchedPlugin, "read" | "list">;
44
+ /** Load and validate the plugin manifest in `dir`, exposing its files for adapters. */
45
+ declare function loadPluginDir(dir: string): ParseResult<FetchedPlugin>;
46
+ /** True when `dir` is a marketplace (a catalog of plugins) rather than a plugin. */
47
+ declare function hasMarketplaceManifest(dir: string): boolean;
48
+ /** Load and validate the marketplace manifest in `dir`. */
49
+ declare function loadMarketplaceDir(dir: string): ParseResult<FetchedMarketplace>;
50
+
51
+ /**
52
+ * Adapters registered by Target (spec §7). Core depends only on the adapter-kit
53
+ * interface; the CLI (or an embedding app) registers concrete adapters, keeping
54
+ * the dependency direction one-way and letting community adapters slot in.
55
+ */
56
+ declare class AdapterRegistry {
57
+ private readonly adapters;
58
+ register(adapter: HarnessAdapter): this;
59
+ get(target: Target): HarnessAdapter | undefined;
60
+ has(target: Target): boolean;
61
+ get targets(): Target[];
62
+ }
63
+
64
+ /** An emitted artifact tagged with the canonical component it came from. */
65
+ interface TaggedArtifact {
66
+ componentId: string;
67
+ artifact: CompiledArtifact;
68
+ }
69
+ /** Everything one adapter produced for one plugin. */
70
+ interface TargetOutput {
71
+ target: Target;
72
+ adapter: HarnessAdapter;
73
+ /** Plugin-root-relative artifacts: components + the plugin manifest. */
74
+ artifacts: TaggedArtifact[];
75
+ }
76
+ interface CompileResult {
77
+ fb: FetchedPlugin;
78
+ /** Plugin id: `{namespace}/{name}`. */
79
+ id: string;
80
+ /** The components actually compiled (the piecemeal selection, or all). */
81
+ components: ResolvedComponent[];
82
+ aliases: Record<string, string>;
83
+ diagnostics: Diagnostics;
84
+ targets: TargetOutput[];
85
+ }
86
+ interface CompileOptions {
87
+ registry: AdapterRegistry;
88
+ /** Restrict to these targets; default = every target with a registered adapter. */
89
+ targets?: Target[];
90
+ /** Piecemeal: include only these component leaf names; default = all (spec §9.2). */
91
+ only?: string[];
92
+ }
93
+ /** A synthetic one-entry marketplace wrapping a single plugin (spec §9.1 build). */
94
+ declare function synthMarketplace(plugin: Plugin): ResolvedMarketplace;
95
+ interface ResolvedComponent {
96
+ id: string;
97
+ leaf: string;
98
+ kind: ComponentKind;
99
+ component: Component;
100
+ }
101
+ interface StaticPass {
102
+ id: string;
103
+ diagnostics: Diagnostics;
104
+ aliases: Record<string, string>;
105
+ resolved: ResolvedComponent[];
106
+ }
107
+ /**
108
+ * Steps 1-4 of the pipeline shared by `compile` and `lint`: min-version,
109
+ * static validation, fully-qualified ids, and alias resolution. No adapters run,
110
+ * so it is the deterministic "is this plugin valid?" pass behind the valid badge.
111
+ */
112
+ declare function staticPass(fb: FetchedPlugin): StaticPass;
113
+ /**
114
+ * Steps 1-5 of the compile pipeline (spec §9.1): load is done by the caller;
115
+ * here we enforce min-version, validate statically, resolve aliases, and run each
116
+ * adapter's transform + emitManifest. Catalog emission (a marketplace concern) and
117
+ * placement are separate so `build` can inspect without installing and `install`
118
+ * can pull components piecemeal.
119
+ *
120
+ * Never throws on plugin problems -- it accumulates diagnostics so the caller can
121
+ * render them. `build`/`install` fail closed when `diagnostics.hasErrors`.
122
+ */
123
+ declare function compile(fb: FetchedPlugin, opts: CompileOptions): CompileResult;
124
+
125
+ interface ConfigResolution {
126
+ env: string;
127
+ /** Where the value came from -- never the value itself. */
128
+ source: "env" | "default" | "missing";
129
+ secret: boolean;
130
+ }
131
+ interface SecretsResult {
132
+ resolved: ConfigResolution[];
133
+ /** Path to the gitignored local config the values were written to (or null). */
134
+ path: string | null;
135
+ }
136
+ /**
137
+ * Resolve declared `ConfigVar`s (spec §9.1 step 7, §11 rule 4: declare-not-store).
138
+ * Values come from the environment or a declared default and are written ONLY to a
139
+ * local, gitignored config -- never to the lockfile, the plugin, the index, or
140
+ * telemetry. The returned summary records where each value came from, not the value.
141
+ */
142
+ declare function resolveConfig(plugin: Plugin, cwd: string, env?: Record<string, string | undefined>): SecretsResult;
143
+
144
+ /**
145
+ * A managed-mode install policy (spec §11 rule 5): the same install mechanism as
146
+ * a solo dev, restricted by scope + policy. An enterprise pins an allowlist of
147
+ * namespaces and/or required badges; only scope and policy differ, never mechanism.
148
+ */
149
+ interface ManagedPolicy {
150
+ /** Only these reverse-DNS namespaces may be installed. */
151
+ allowNamespaces?: string[];
152
+ /** Each installed plugin must carry all of these badges. */
153
+ requireBadges?: Badge[];
154
+ }
155
+ interface PolicyContext {
156
+ namespace: string;
157
+ badges?: Badge[];
158
+ }
159
+ /** A blocking reason when the policy forbids the install, or null when permitted. */
160
+ declare function checkManagedPolicy(policy: ManagedPolicy | undefined, ctx: PolicyContext): string | null;
161
+
162
+ interface WrittenArtifact {
163
+ target: Target;
164
+ /** Relative to the per-target output base. */
165
+ relPath: string;
166
+ abs: string;
167
+ hash: string;
168
+ kind?: ArtifactKind;
169
+ }
170
+ /** Write one plugin's artifacts under `<baseDir>/plugins/<pluginName>/`. */
171
+ declare function placePluginArtifacts(target: TargetOutput, baseDir: string, pluginName: string): WrittenArtifact[];
172
+ /** Emit and write a harness's native catalog at `<baseDir>/`. */
173
+ declare function placeCatalog(adapter: HarnessAdapter, marketplace: ResolvedMarketplace, baseDir: string): WrittenArtifact[];
174
+ /**
175
+ * `loom build` placement for a single plugin (spec §9.1 step 6, inspect-only):
176
+ * writes the plugin tree at `outDir/<target>/plugins/<plugin>/` plus a synthetic
177
+ * one-entry catalog at the target root, leaving harness install dirs untouched.
178
+ */
179
+ declare function buildToDir(result: CompileResult, outDir: string): WrittenArtifact[];
180
+ interface PlannedArtifact {
181
+ record: ArtifactRecord;
182
+ contents: Buffer;
183
+ }
184
+ /**
185
+ * Compute (without writing) where each target's plugin tree would land in the
186
+ * scope, with content hashes. Drives both install (write all) and update (write
187
+ * only what changed). Executable/passthrough artifacts are recorded DISABLED (§11).
188
+ */
189
+ declare function planScopeArtifacts(result: CompileResult, scope: Scope, cwd: string): PlannedArtifact[];
190
+ /** `loom install` placement (spec §9.1 step 6): write every artifact, record each. */
191
+ declare function installToScope(result: CompileResult, scope: Scope, cwd: string): ArtifactRecord[];
192
+
193
+ interface LintResult {
194
+ id: string;
195
+ plugin: Plugin;
196
+ aliases: Record<string, string>;
197
+ diagnostics: Diagnostics;
198
+ }
199
+ /** Load + statically validate a plugin without running any adapter (the valid badge). */
200
+ declare function lint(pluginDir: string): LintResult;
201
+ interface BuildOptions {
202
+ pluginDir: string;
203
+ outDir: string;
204
+ registry: AdapterRegistry;
205
+ targets?: Target[];
206
+ }
207
+ interface BuildResult {
208
+ result: CompileResult;
209
+ written: WrittenArtifact[];
210
+ }
211
+ /** Compile a plugin and write its marketplace + plugin layout into `outDir` (no install). */
212
+ declare function build(opts: BuildOptions): Promise<BuildResult>;
213
+ interface BuildMarketplaceOptions {
214
+ marketplaceDir: string;
215
+ outDir: string;
216
+ registry: AdapterRegistry;
217
+ targets?: Target[];
218
+ }
219
+ interface BuildMarketplaceResult {
220
+ marketplace: Marketplace;
221
+ plugins: CompileResult[];
222
+ written: WrittenArtifact[];
223
+ }
224
+ /**
225
+ * Compile a curated `marketplace.yaml` of many plugins: resolve and compile each
226
+ * referenced plugin, place every plugin tree under `outDir/<target>/plugins/`,
227
+ * and emit ONE native catalog per target listing them all (spec §6.2). This is
228
+ * the company-marketplace workflow.
229
+ */
230
+ declare function buildMarketplace(opts: BuildMarketplaceOptions): Promise<BuildMarketplaceResult>;
231
+ interface InstallOptions {
232
+ pluginDir: string;
233
+ scope: Scope;
234
+ cwd: string;
235
+ registry: AdapterRegistry;
236
+ targets?: Target[];
237
+ /** Piecemeal: install only these component leaf names (spec §9.2). */
238
+ only?: string[];
239
+ /** Managed-mode policy: namespace allowlist / required badges (spec §11). */
240
+ managed?: ManagedPolicy;
241
+ /** Badges known for this plugin (for managed `requireBadges`). */
242
+ badges?: Badge[];
243
+ /** Where to write `loom.lock` (default: the plugin dir). Eval points this at a scratch dir. */
244
+ lockDir?: string;
245
+ /** Inject the lockfile timestamp for deterministic tests. */
246
+ now?: string;
247
+ }
248
+ interface InstallResult {
249
+ result: CompileResult;
250
+ lockfile: Lockfile;
251
+ lockPath: string;
252
+ /** Declared config resolution summary (never the values) -- spec §11. */
253
+ secrets: SecretsResult;
254
+ }
255
+ /** Compile a plugin, place it into the scope's dirs, resolve config, write `loom.lock`. */
256
+ declare function install(opts: InstallOptions): Promise<InstallResult>;
257
+ interface UninstallOptions {
258
+ /** Where loom.lock lives (also the default place uninstall reads from). */
259
+ pluginDir: string;
260
+ lockDir?: string;
261
+ }
262
+ interface UninstallResult {
263
+ removed: string[];
264
+ }
265
+ /**
266
+ * Remove everything `install` placed, using the paths recorded in `loom.lock`,
267
+ * then delete the lockfile (spec §6.3). Errors are defined out of existence:
268
+ * a missing artifact is simply skipped.
269
+ */
270
+ declare function uninstall(opts: UninstallOptions): UninstallResult;
271
+ interface UpdateResult {
272
+ lockfile: Lockfile;
273
+ lockPath: string;
274
+ /** Artifacts whose content hash changed (or are new) since the prior lockfile. */
275
+ changed: string[];
276
+ }
277
+ /**
278
+ * Re-resolve refs, recompile, diff artifact content hashes against `loom.lock`,
279
+ * and re-place ONLY changed artifacts (spec §5, §9.3). Content addressing makes
280
+ * "is there really a new version?" exact: an unchanged artifact is never rewritten.
281
+ */
282
+ declare function update(opts: InstallOptions): Promise<UpdateResult>;
283
+
284
+ interface DependencyRecord {
285
+ id: string;
286
+ range: string;
287
+ resolvedSha: string;
288
+ }
289
+ interface ResolvedDeps {
290
+ fb: FetchedPlugin;
291
+ dependencies: DependencyRecord[];
292
+ }
293
+ /**
294
+ * Resolve a plugin's `depends` (spec §9.1 step 2). Each dependency is fetched
295
+ * (locally or git-cloned and pinned to a SHA), then its selected components are
296
+ * vendored into a merged temp tree under `_deps/<name>/` and registered under the
297
+ * consuming plugin's namespace. Piecemeal `components:[…]` selects which to
298
+ * register, but the whole dep tree is copied so shared assets travel (drift-aware).
299
+ * Direct cycles are detected by id.
300
+ */
301
+ declare function resolveDependencies(fb: FetchedPlugin, tmpRoot?: string): Promise<ResolvedDeps>;
302
+
303
+ /** sha256 of compiled output, used for content-addressed update + the signed badge. */
304
+ declare function sha256(contents: string | Buffer): string;
305
+
306
+ interface ImportPluginOptions {
307
+ /** Directory holding an existing native plugin or marketplace. */
308
+ dir: string;
309
+ adapter: HarnessAdapter;
310
+ /** Where to write the generated Loom plugin/marketplace. */
311
+ outDir: string;
312
+ /** Reverse-DNS namespace to assign (native assets lack one). */
313
+ namespace?: string;
314
+ }
315
+ interface ImportOutput {
316
+ kind: "plugin" | "marketplace";
317
+ name: string;
318
+ outDir: string;
319
+ manifestPath: string;
320
+ fileCount: number;
321
+ id?: string;
322
+ }
323
+ /**
324
+ * Reverse-compile an existing native plugin/marketplace into the Loom model and
325
+ * write it to `outDir`, ready for `loom build` to cross-compile to every other
326
+ * harness. This is "federate, don't wall off" applied to assets you already have.
327
+ */
328
+ declare function importNativePlugin(opts: ImportPluginOptions): ImportOutput;
329
+
330
+ interface LockInput {
331
+ result: CompileResult;
332
+ artifacts: ArtifactRecord[];
333
+ ref: string;
334
+ sha: string;
335
+ generatedAt: string;
336
+ dependencies?: Lockfile["dependencies"];
337
+ }
338
+ /** Assemble the `loom.lock` object (spec §6.3) from an install. */
339
+ declare function buildLockfile(input: LockInput): Lockfile;
340
+ declare function serializeLock(lock: Lockfile): string;
341
+ declare function writeLock(dir: string, lock: Lockfile): string;
342
+ /** Read and validate an existing lockfile; null when absent or malformed. */
343
+ declare function readLock(dir: string): Lockfile | null;
344
+
345
+ /** One component identified for aliasing: its bare leaf name and full id. */
346
+ interface AliasInput {
347
+ id: string;
348
+ leaf: string;
349
+ }
350
+ interface AliasResult {
351
+ /** Bare leaf name -> fully-qualified id. */
352
+ aliases: Record<string, string>;
353
+ /** True same-scope collisions: one leaf claimed by multiple ids. */
354
+ collisions: Array<{
355
+ leaf: string;
356
+ ids: string[];
357
+ }>;
358
+ }
359
+ /**
360
+ * Bare name when unambiguous (spec §9.4). A leaf used by exactly one component
361
+ * gets the bare alias; a leaf shared by several is surfaced as a collision for
362
+ * the caller to resolve (prompt interactively, or error non-interactively).
363
+ * Never silently last-wins.
364
+ */
365
+ declare function resolveAliases(components: AliasInput[]): AliasResult;
366
+
367
+ type Source = {
368
+ kind: "local";
369
+ path: string;
370
+ } | {
371
+ kind: "github";
372
+ repo: string;
373
+ ref?: string;
374
+ } | {
375
+ kind: "git";
376
+ url: string;
377
+ ref?: string;
378
+ } | {
379
+ kind: "npm";
380
+ pkg: string;
381
+ version?: string;
382
+ };
383
+ /** Where cloned/fetched remote plugins are cached. */
384
+ declare const CACHE_DIR: string;
385
+ /** Parse a plugin/dependency source string into a structured form (spec §6.1). */
386
+ declare function parseSource(src: string): Source;
387
+ /** A fetched plugin plus the ref/SHA it resolved to (for the lockfile). */
388
+ interface ResolvedPlugin {
389
+ fb: FetchedPlugin;
390
+ ref: string;
391
+ sha: string;
392
+ }
393
+ /**
394
+ * Resolve a plugin source to a fetched plugin on disk (spec §5, §9.1 step 2).
395
+ * Local `./path` sources resolve relative to `fromRoot`; `github:`/git sources are
396
+ * git-cloned into the cache and pinned to a resolved SHA; `npm:` is a clear stub.
397
+ */
398
+ declare function resolvePluginRefFull(source: string, fromRoot: string): Promise<ResolvedPlugin>;
399
+ /** Convenience: resolve and return only the fetched plugin. */
400
+ declare function resolvePluginRef(source: string, fromRoot: string): Promise<FetchedPlugin>;
401
+ /** Resolve a `depends` entry to a fetched plugin (relative to the depending plugin root). */
402
+ declare function resolveDependency(dep: Dependency, fromRoot: string): Promise<ResolvedPlugin>;
403
+ /** Best-effort git ref + SHA for the lockfile. Returns sentinels outside a repo. */
404
+ declare function gitInfo(dir: string): Promise<{
405
+ ref: string;
406
+ sha: string;
407
+ }>;
408
+
409
+ /**
410
+ * A digest over the lockfile's artifact set (component + target + content hash).
411
+ * Signing this binds a signature to exactly the compiled artifacts (spec §10
412
+ * `signed` badge). Sorting makes it order-independent and deterministic.
413
+ */
414
+ declare function artifactDigest(lock: Lockfile): Buffer;
415
+ /** Ed25519 keypair. Production would use sigstore/cosign keyless signing instead. */
416
+ declare function generateSigningKeys(): {
417
+ publicKey: KeyObject;
418
+ privateKey: KeyObject;
419
+ };
420
+ declare function signLock(lock: Lockfile, privateKey: KeyObject): string;
421
+ declare function verifyLockSignature(lock: Lockfile, publicKey: KeyObject, signature: string): boolean;
422
+ interface VerifyResult {
423
+ signatureValid: boolean;
424
+ /** Recorded artifact paths whose on-disk bytes no longer match the lock hash. */
425
+ tampered: string[];
426
+ }
427
+ /**
428
+ * Verify a signature AND that every recorded artifact on disk still matches its
429
+ * lockfile hash. The `signed` badge requires both: a valid signature over the
430
+ * digest and no tampered artifacts.
431
+ */
432
+ declare function verifyArtifacts(lock: Lockfile, publicKey: KeyObject, signature: string): VerifyResult;
433
+
434
+ /**
435
+ * Static validation (spec §9.1 step 3, the `valid` badge): every referenced file
436
+ * exists, skill/agent frontmatter is well-formed, `server.json` parses, and
437
+ * descriptions clear a basic quality bar. Errors fail the compile closed.
438
+ */
439
+ declare function validatePlugin(fb: FetchedPlugin, diags: Diagnostics): void;
440
+
441
+ /** The Loom/CLI version (versioning axis 2, spec §5). */
442
+ declare const LOOM_VERSION = "0.1.0";
443
+ /**
444
+ * Check a plugin's `loom_min_version` against the running Loom. Returns an error
445
+ * message when unmet, or null when satisfied / unspecified.
446
+ */
447
+ declare function checkMinVersion(min: string | undefined): string | null;
448
+
449
+ export { AdapterRegistry, type AliasInput, type AliasResult, type BuildMarketplaceOptions, type BuildMarketplaceResult, type BuildOptions, type BuildResult, CACHE_DIR, CompileError, type CompileOptions, type CompileResult, type ConfigResolution, type DependencyRecord, type Diagnostic, Diagnostics, type FetchedMarketplace, type FetchedPlugin, type ImportOutput, type ImportPluginOptions, type InstallOptions, type InstallResult, LOOM_VERSION, type LintResult, type LockInput, type ManagedPolicy, type PlannedArtifact, type PolicyContext, type ResolvedComponent, type ResolvedDeps, type ResolvedPlugin, type SecretsResult, type Severity, type Source, type StaticPass, type TaggedArtifact, type TargetOutput, type UninstallOptions, type UninstallResult, type UpdateResult, type VerifyResult, type WrittenArtifact, artifactDigest, build, buildLockfile, buildMarketplace, buildToDir, checkManagedPolicy, checkMinVersion, compile, fileAccessors, generateSigningKeys, gitInfo, hasMarketplaceManifest, importNativePlugin, install, installToScope, lint, loadMarketplaceDir, loadPluginDir, parseSource, placeCatalog, placePluginArtifacts, planScopeArtifacts, readLock, resolveAliases, resolveConfig, resolveDependencies, resolveDependency, resolvePluginRef, resolvePluginRefFull, serializeLock, sha256, signLock, staticPass, synthMarketplace, uninstall, update, validatePlugin, verifyArtifacts, verifyLockSignature, writeLock };