agent-package-manager 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -15
- package/cli-contract.yaml +38 -25
- package/dist/cli/index.js +280 -140
- package/dist/cli/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -30,7 +30,7 @@ function createProgram(handlers2, version) {
|
|
|
30
30
|
program2.command("check").description("Run full verification pipeline for CI.").action(async (_opts, cmd) => {
|
|
31
31
|
await handlers2.check(cmd.optsWithGlobals());
|
|
32
32
|
});
|
|
33
|
-
program2.command("publish").description("Publish a package
|
|
33
|
+
program2.command("publish").description("Publish a package to a registry repository.").argument("<path>", "Path to the local package directory to publish.").option("--version <semver>", "Override version for this publish.").option("-r, --registry <url>", "Target registry in the format github:<owner>/<repo>.").option("--name <name>", "Override package name.").option("--dry-run", "Show what would be published without making changes.", false).option("--no-push", "Commit locally but do not push to remote.", false).action(async (path, opts, cmd) => {
|
|
34
34
|
await handlers2.publish(path, opts, cmd.optsWithGlobals());
|
|
35
35
|
});
|
|
36
36
|
return program2;
|
|
@@ -116,15 +116,6 @@ function parseRegistrySpec(spec) {
|
|
|
116
116
|
}
|
|
117
117
|
return { owner, repo };
|
|
118
118
|
}
|
|
119
|
-
function buildTag(packageName, version, tagFormat) {
|
|
120
|
-
const fmt = tagFormat || "{package}/v{version}";
|
|
121
|
-
return fmt.replace("{package}", packageName).replace("{version}", version);
|
|
122
|
-
}
|
|
123
|
-
function parseTagVersion(tag, packageName) {
|
|
124
|
-
const prefix = `${packageName}/v`;
|
|
125
|
-
if (!tag.startsWith(prefix)) return null;
|
|
126
|
-
return tag.slice(prefix.length);
|
|
127
|
-
}
|
|
128
119
|
function resolveRegistryForPackage(config, packageName) {
|
|
129
120
|
const pkg = config.packages[packageName];
|
|
130
121
|
const registryName = pkg?.registry || "default";
|
|
@@ -142,6 +133,7 @@ var DEFAULT_MODULES_DIR = "agent_modules";
|
|
|
142
133
|
var DEFAULT_BUILD_DIR = ".agent-pkg/build";
|
|
143
134
|
var DEFAULT_CACHE_DIR = ".agent-pkg/cache";
|
|
144
135
|
var PACKAGE_MANIFEST_FILE = "agent-pkg.package.json";
|
|
136
|
+
var REGISTRY_MANIFEST_FILE = "agent-pkg.registry.json";
|
|
145
137
|
|
|
146
138
|
// src/lib/commands/init.ts
|
|
147
139
|
async function runInit(opts, projectDir) {
|
|
@@ -228,8 +220,8 @@ active_guardrail_policy: default
|
|
|
228
220
|
|
|
229
221
|
// src/lib/commands/add.ts
|
|
230
222
|
import { existsSync as existsSync4 } from "fs";
|
|
231
|
-
import { mkdir as mkdir3, rm, readFile as
|
|
232
|
-
import { join as
|
|
223
|
+
import { mkdir as mkdir3, rm, readFile as readFile4, writeFile as writeFile4 } from "fs/promises";
|
|
224
|
+
import { join as join6 } from "path";
|
|
233
225
|
|
|
234
226
|
// src/lib/config.ts
|
|
235
227
|
import { readFile, writeFile as writeFile2 } from "fs/promises";
|
|
@@ -302,6 +294,9 @@ function getCacheBaseDir() {
|
|
|
302
294
|
function getRepoCacheDir(owner, repo) {
|
|
303
295
|
return join3(getCacheBaseDir(), "github.com", owner, repo);
|
|
304
296
|
}
|
|
297
|
+
function getWorkingCopyDir(owner, repo) {
|
|
298
|
+
return join3(getCacheBaseDir(), "github.com", owner, `${repo}__work`);
|
|
299
|
+
}
|
|
305
300
|
function repoUrl(owner, repo) {
|
|
306
301
|
return `https://github.com/${owner}/${repo}.git`;
|
|
307
302
|
}
|
|
@@ -309,7 +304,7 @@ async function ensureCloned(owner, repo) {
|
|
|
309
304
|
const dir = getRepoCacheDir(owner, repo);
|
|
310
305
|
if (existsSync3(join3(dir, "HEAD"))) {
|
|
311
306
|
log.debug(`Cache hit: ${dir}`);
|
|
312
|
-
await
|
|
307
|
+
await fetchAll(dir);
|
|
313
308
|
return dir;
|
|
314
309
|
}
|
|
315
310
|
const parent = join3(dir, "..");
|
|
@@ -319,24 +314,16 @@ async function ensureCloned(owner, repo) {
|
|
|
319
314
|
await git(["clone", "--bare", url, dir]);
|
|
320
315
|
return dir;
|
|
321
316
|
}
|
|
322
|
-
async function
|
|
323
|
-
log.debug(`Fetching
|
|
324
|
-
await git(["fetch", "--
|
|
325
|
-
}
|
|
326
|
-
async function resolveTag(repoPath, tag) {
|
|
327
|
-
log.debug(`Resolving tag: ${tag}`);
|
|
328
|
-
return git(["rev-parse", `${tag}^{commit}`], repoPath);
|
|
317
|
+
async function fetchAll(repoPath) {
|
|
318
|
+
log.debug(`Fetching in ${repoPath}`);
|
|
319
|
+
await git(["fetch", "--prune", "--force"], repoPath);
|
|
329
320
|
}
|
|
330
|
-
async function
|
|
331
|
-
|
|
332
|
-
const output = await git(["tag", "--list", `${prefix}*`], repoPath);
|
|
333
|
-
if (!output) return [];
|
|
334
|
-
return output.split("\n").filter((t) => t.startsWith(prefix)).sort();
|
|
321
|
+
async function readFileFromRepo(repoPath, filePath, ref = "HEAD") {
|
|
322
|
+
return git(["show", `${ref}:${filePath}`], repoPath);
|
|
335
323
|
}
|
|
336
|
-
async function
|
|
337
|
-
log.debug(`Extracting ${packagePath} at ${
|
|
324
|
+
async function extractPackageAtCommit(repoPath, commit, packagePath, destDir) {
|
|
325
|
+
log.debug(`Extracting ${packagePath} at ${commit.slice(0, 12)} \u2192 ${destDir}`);
|
|
338
326
|
await mkdir2(destDir, { recursive: true });
|
|
339
|
-
const commit = await resolveTag(repoPath, tag);
|
|
340
327
|
const stripCount = packagePath.split("/").length;
|
|
341
328
|
return new Promise((resolve2, reject) => {
|
|
342
329
|
const gitProc = spawn(
|
|
@@ -366,20 +353,114 @@ async function extractPackageSimple(repoPath, tag, packagePath, destDir) {
|
|
|
366
353
|
});
|
|
367
354
|
});
|
|
368
355
|
}
|
|
369
|
-
async function
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
356
|
+
async function ensureWorkingCopy(owner, repo) {
|
|
357
|
+
const dir = getWorkingCopyDir(owner, repo);
|
|
358
|
+
if (existsSync3(join3(dir, ".git"))) {
|
|
359
|
+
log.debug(`Working copy hit: ${dir}`);
|
|
360
|
+
await git(["fetch", "origin"], dir);
|
|
361
|
+
await git(["reset", "--hard", "origin/main"], dir);
|
|
362
|
+
await git(["clean", "-fd"], dir);
|
|
363
|
+
return dir;
|
|
364
|
+
}
|
|
365
|
+
const parent = join3(dir, "..");
|
|
366
|
+
await mkdir2(parent, { recursive: true });
|
|
367
|
+
const url = repoUrl(owner, repo);
|
|
368
|
+
log.step(`Cloning working copy: ${url}`);
|
|
369
|
+
await git(["clone", url, dir]);
|
|
370
|
+
await git(["config", "user.email", "agent-pkg@automated"], dir);
|
|
371
|
+
await git(["config", "user.name", "agent-pkg"], dir);
|
|
372
|
+
return dir;
|
|
374
373
|
}
|
|
375
|
-
async function
|
|
374
|
+
async function commitAll(cwd2, message) {
|
|
375
|
+
await git(["add", "-A"], cwd2);
|
|
376
|
+
const status = await git(["status", "--porcelain"], cwd2);
|
|
377
|
+
if (!status) {
|
|
378
|
+
throw new Error("Nothing to commit \u2014 no changes detected");
|
|
379
|
+
}
|
|
380
|
+
await git(["commit", "-m", message], cwd2);
|
|
376
381
|
return git(["rev-parse", "HEAD"], cwd2);
|
|
377
382
|
}
|
|
383
|
+
async function pushBranch(cwd2, remote = "origin", branch = "main") {
|
|
384
|
+
await git(["push", remote, branch], cwd2);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/lib/registry.ts
|
|
388
|
+
import { readFile as readFile2, writeFile as writeFile3 } from "fs/promises";
|
|
389
|
+
import { join as join4 } from "path";
|
|
390
|
+
function createEmptyManifest(name) {
|
|
391
|
+
return {
|
|
392
|
+
schemaVersion: 1,
|
|
393
|
+
name,
|
|
394
|
+
packages: {}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
async function readManifestFromRepo(repoPath) {
|
|
398
|
+
try {
|
|
399
|
+
const content = await readFileFromRepo(repoPath, REGISTRY_MANIFEST_FILE);
|
|
400
|
+
return JSON.parse(content);
|
|
401
|
+
} catch {
|
|
402
|
+
throw new Error(
|
|
403
|
+
`Registry manifest (${REGISTRY_MANIFEST_FILE}) not found. The registry repository must contain an ${REGISTRY_MANIFEST_FILE} at its root.`
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
async function readManifestFromDir(dir) {
|
|
408
|
+
const path = join4(dir, REGISTRY_MANIFEST_FILE);
|
|
409
|
+
const content = await readFile2(path, "utf8");
|
|
410
|
+
return JSON.parse(content);
|
|
411
|
+
}
|
|
412
|
+
async function writeManifestToDir(dir, manifest) {
|
|
413
|
+
const path = join4(dir, REGISTRY_MANIFEST_FILE);
|
|
414
|
+
await writeFile3(path, JSON.stringify(manifest, null, 2) + "\n", "utf8");
|
|
415
|
+
}
|
|
416
|
+
function resolveVersionFromManifest(manifest, packageName, version) {
|
|
417
|
+
const pkg = manifest.packages[packageName];
|
|
418
|
+
if (!pkg) {
|
|
419
|
+
const available = Object.keys(manifest.packages);
|
|
420
|
+
throw new Error(
|
|
421
|
+
`Package "${packageName}" not found in registry manifest. Available: ${available.join(", ") || "(none)"}`
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
const ver = pkg.versions[version];
|
|
425
|
+
if (!ver) {
|
|
426
|
+
const available = Object.keys(pkg.versions);
|
|
427
|
+
throw new Error(
|
|
428
|
+
`Version "${version}" not found for package "${packageName}". Available: ${available.join(", ") || "(none)"}`
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
return { commit: ver.commit, contentHash: ver.contentHash, path: pkg.path };
|
|
432
|
+
}
|
|
433
|
+
function listVersionsFromManifest(manifest, packageName) {
|
|
434
|
+
const pkg = manifest.packages[packageName];
|
|
435
|
+
if (!pkg) return [];
|
|
436
|
+
return Object.keys(pkg.versions);
|
|
437
|
+
}
|
|
438
|
+
function addVersionToManifest(manifest, packageName, packagePath, version, commit, contentHash, description) {
|
|
439
|
+
if (!manifest.packages[packageName]) {
|
|
440
|
+
manifest.packages[packageName] = {
|
|
441
|
+
path: packagePath,
|
|
442
|
+
description,
|
|
443
|
+
versions: {}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
const pkg = manifest.packages[packageName];
|
|
447
|
+
if (pkg.versions[version]) {
|
|
448
|
+
throw new Error(
|
|
449
|
+
`Version "${version}" already exists for package "${packageName}". Bump the version before publishing.`
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
pkg.versions[version] = {
|
|
453
|
+
commit,
|
|
454
|
+
contentHash,
|
|
455
|
+
publishedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
456
|
+
};
|
|
457
|
+
log.debug(`Registered ${packageName}@${version} in manifest`);
|
|
458
|
+
}
|
|
378
459
|
|
|
379
460
|
// src/lib/hash.ts
|
|
380
461
|
import { createHash } from "crypto";
|
|
381
|
-
import { readFile as
|
|
382
|
-
import { join as
|
|
462
|
+
import { readFile as readFile3, readdir } from "fs/promises";
|
|
463
|
+
import { join as join5, relative } from "path";
|
|
383
464
|
async function hashDirectory(dirPath) {
|
|
384
465
|
const files = await collectFiles(dirPath);
|
|
385
466
|
files.sort();
|
|
@@ -387,7 +468,7 @@ async function hashDirectory(dirPath) {
|
|
|
387
468
|
for (const file of files) {
|
|
388
469
|
const rel = relative(dirPath, file);
|
|
389
470
|
hash.update(rel);
|
|
390
|
-
const content = await
|
|
471
|
+
const content = await readFile3(file);
|
|
391
472
|
hash.update(content);
|
|
392
473
|
}
|
|
393
474
|
return `sha256:${hash.digest("hex")}`;
|
|
@@ -396,7 +477,7 @@ async function collectFiles(dir) {
|
|
|
396
477
|
const results = [];
|
|
397
478
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
398
479
|
for (const entry of entries) {
|
|
399
|
-
const full =
|
|
480
|
+
const full = join5(dir, entry.name);
|
|
400
481
|
if (entry.isDirectory()) {
|
|
401
482
|
results.push(...await collectFiles(full));
|
|
402
483
|
} else if (entry.isFile()) {
|
|
@@ -418,10 +499,8 @@ async function runAdd(input, projectDir) {
|
|
|
418
499
|
}
|
|
419
500
|
const packageName = input.alias || spec.name;
|
|
420
501
|
let registry;
|
|
421
|
-
let packagePath;
|
|
422
502
|
if (spec.owner && spec.repo) {
|
|
423
503
|
registry = { type: "github", owner: spec.owner, repo: spec.repo };
|
|
424
|
-
packagePath = spec.path || `packages/${spec.name}`;
|
|
425
504
|
} else {
|
|
426
505
|
const registryName2 = input.registry || "default";
|
|
427
506
|
registry = config.registries[registryName2];
|
|
@@ -430,29 +509,31 @@ async function runAdd(input, projectDir) {
|
|
|
430
509
|
`Registry "${registryName2}" not found. Available: ${Object.keys(config.registries).join(", ") || "(none \u2014 run init with --registry)"}`
|
|
431
510
|
);
|
|
432
511
|
}
|
|
433
|
-
const root = registry.packageRoot || "packages";
|
|
434
|
-
packagePath = `${root}/${spec.name}`;
|
|
435
512
|
}
|
|
436
|
-
|
|
437
|
-
const tag = buildTag(spec.name, spec.version, tagFormat);
|
|
438
|
-
log.step(`Resolving ${spec.name}@${spec.version} (tag: ${tag})`);
|
|
513
|
+
log.step(`Resolving ${spec.name}@${spec.version} from manifest`);
|
|
439
514
|
const repoDir = await ensureCloned(registry.owner, registry.repo);
|
|
440
|
-
const
|
|
441
|
-
|
|
515
|
+
const manifest = await readManifestFromRepo(repoDir);
|
|
516
|
+
const resolved = resolveVersionFromManifest(manifest, spec.name, spec.version);
|
|
517
|
+
log.step(`Resolved commit: ${resolved.commit.slice(0, 12)}`);
|
|
442
518
|
const modulesDir = config.install.modulesDir;
|
|
443
|
-
const installDir =
|
|
519
|
+
const installDir = join6(projectDir, modulesDir, packageName);
|
|
444
520
|
if (existsSync4(installDir)) {
|
|
445
521
|
await rm(installDir, { recursive: true, force: true });
|
|
446
522
|
}
|
|
447
523
|
await mkdir3(installDir, { recursive: true });
|
|
448
|
-
log.step(`Extracting ${
|
|
449
|
-
await
|
|
524
|
+
log.step(`Extracting ${resolved.path} \u2192 ${modulesDir}/${packageName}`);
|
|
525
|
+
await extractPackageAtCommit(repoDir, resolved.commit, resolved.path, installDir);
|
|
450
526
|
const contentHash = await hashDirectory(installDir);
|
|
451
527
|
log.debug(`Content hash: ${contentHash}`);
|
|
528
|
+
if (contentHash !== resolved.contentHash) {
|
|
529
|
+
log.warn(
|
|
530
|
+
`Content hash mismatch: manifest=${resolved.contentHash}, extracted=${contentHash}. The registry manifest may be stale.`
|
|
531
|
+
);
|
|
532
|
+
}
|
|
452
533
|
const registryName = input.registry || "default";
|
|
453
534
|
config.packages[packageName] = {
|
|
454
535
|
registry: registryName,
|
|
455
|
-
path:
|
|
536
|
+
path: resolved.path,
|
|
456
537
|
version: spec.version
|
|
457
538
|
};
|
|
458
539
|
if (!config.registries[registryName]) {
|
|
@@ -465,7 +546,7 @@ async function runAdd(input, projectDir) {
|
|
|
465
546
|
};
|
|
466
547
|
}
|
|
467
548
|
await writeProjectConfig(projectDir, config);
|
|
468
|
-
log.success(`Updated
|
|
549
|
+
log.success(`Updated config with ${packageName}@${spec.version}`);
|
|
469
550
|
let lock;
|
|
470
551
|
try {
|
|
471
552
|
lock = await readLockFile(projectDir);
|
|
@@ -486,10 +567,10 @@ async function runAdd(input, projectDir) {
|
|
|
486
567
|
};
|
|
487
568
|
lock.packages[packageName] = {
|
|
488
569
|
registry: registryName,
|
|
489
|
-
path:
|
|
570
|
+
path: resolved.path,
|
|
490
571
|
version: spec.version,
|
|
491
|
-
tag
|
|
492
|
-
commit,
|
|
572
|
+
tag: `${spec.name}/v${spec.version}`,
|
|
573
|
+
commit: resolved.commit,
|
|
493
574
|
contentHash,
|
|
494
575
|
installedTo: `${modulesDir}/${packageName}`,
|
|
495
576
|
entrypoint: `${modulesDir}/${packageName}/agent-contracts.yaml`
|
|
@@ -500,8 +581,7 @@ async function runAdd(input, projectDir) {
|
|
|
500
581
|
const result = {
|
|
501
582
|
package: packageName,
|
|
502
583
|
version: spec.version,
|
|
503
|
-
|
|
504
|
-
commit,
|
|
584
|
+
commit: resolved.commit,
|
|
505
585
|
installedTo: `${modulesDir}/${packageName}`,
|
|
506
586
|
materialized: false
|
|
507
587
|
};
|
|
@@ -509,9 +589,9 @@ async function runAdd(input, projectDir) {
|
|
|
509
589
|
console.log(JSON.stringify(result, null, 2));
|
|
510
590
|
}
|
|
511
591
|
async function updateExtendsInYaml(projectDir, modulesDir, packageName) {
|
|
512
|
-
const yamlPath =
|
|
592
|
+
const yamlPath = join6(projectDir, "agent-contracts.yaml");
|
|
513
593
|
if (!existsSync4(yamlPath)) return;
|
|
514
|
-
const content = await
|
|
594
|
+
const content = await readFile4(yamlPath, "utf8");
|
|
515
595
|
const extendEntry = `./${modulesDir}/${packageName}/agent-contracts.yaml`;
|
|
516
596
|
if (content.includes(extendEntry)) return;
|
|
517
597
|
const { parse, stringify } = await import("yaml");
|
|
@@ -520,14 +600,14 @@ async function updateExtendsInYaml(projectDir, modulesDir, packageName) {
|
|
|
520
600
|
if (Array.isArray(doc.extends) && !doc.extends.includes(extendEntry)) {
|
|
521
601
|
doc.extends.push(extendEntry);
|
|
522
602
|
}
|
|
523
|
-
await
|
|
603
|
+
await writeFile4(yamlPath, stringify(doc, { lineWidth: 0 }), "utf8");
|
|
524
604
|
log.success(`Added extends entry: ${extendEntry}`);
|
|
525
605
|
}
|
|
526
606
|
|
|
527
607
|
// src/lib/commands/sync.ts
|
|
528
608
|
import { existsSync as existsSync5 } from "fs";
|
|
529
609
|
import { mkdir as mkdir4, rm as rm2 } from "fs/promises";
|
|
530
|
-
import { join as
|
|
610
|
+
import { join as join7 } from "path";
|
|
531
611
|
async function runSync(input, projectDir) {
|
|
532
612
|
const config = await readProjectConfig(projectDir);
|
|
533
613
|
const lock = await readLockFile(projectDir);
|
|
@@ -538,7 +618,7 @@ async function runSync(input, projectDir) {
|
|
|
538
618
|
if (!regEntry) {
|
|
539
619
|
throw new Error(`Registry "${entry.registry}" referenced by package "${name}" not found in lock file`);
|
|
540
620
|
}
|
|
541
|
-
const installDir =
|
|
621
|
+
const installDir = join7(projectDir, entry.installedTo);
|
|
542
622
|
let currentHash = null;
|
|
543
623
|
if (existsSync5(installDir)) {
|
|
544
624
|
currentHash = await hashDirectory(installDir);
|
|
@@ -561,7 +641,7 @@ async function runSync(input, projectDir) {
|
|
|
561
641
|
await rm2(installDir, { recursive: true, force: true });
|
|
562
642
|
}
|
|
563
643
|
await mkdir4(installDir, { recursive: true });
|
|
564
|
-
await
|
|
644
|
+
await extractPackageAtCommit(repoDir, entry.commit, entry.path, installDir);
|
|
565
645
|
const newHash = await hashDirectory(installDir);
|
|
566
646
|
if (newHash !== entry.contentHash) {
|
|
567
647
|
log.warn(`${name}: restored hash ${newHash} differs from lock ${entry.contentHash}`);
|
|
@@ -586,7 +666,7 @@ async function runSync(input, projectDir) {
|
|
|
586
666
|
// src/lib/commands/update.ts
|
|
587
667
|
import { existsSync as existsSync6 } from "fs";
|
|
588
668
|
import { mkdir as mkdir5, rm as rm3 } from "fs/promises";
|
|
589
|
-
import { join as
|
|
669
|
+
import { join as join8 } from "path";
|
|
590
670
|
async function runUpdate(input, projectDir) {
|
|
591
671
|
const config = await readProjectConfig(projectDir);
|
|
592
672
|
const lock = await readLockFile(projectDir);
|
|
@@ -612,8 +692,9 @@ async function runUpdate(input, projectDir) {
|
|
|
612
692
|
continue;
|
|
613
693
|
}
|
|
614
694
|
const repoDir = await ensureCloned(registry.owner, registry.repo);
|
|
615
|
-
const
|
|
616
|
-
const
|
|
695
|
+
const manifest = await readManifestFromRepo(repoDir);
|
|
696
|
+
const versionStrings = listVersionsFromManifest(manifest, name);
|
|
697
|
+
const versions = versionStrings.map((v) => ({ raw: v, parts: parseSemver(v) })).filter((v) => v.parts !== null).sort((a, b) => compareSemver(a.parts, b.parts));
|
|
617
698
|
const currentParts = parseSemver(pkg.version);
|
|
618
699
|
if (!currentParts) {
|
|
619
700
|
log.warn(`Cannot parse current version "${pkg.version}" for ${name}`);
|
|
@@ -664,21 +745,21 @@ async function runUpdate(input, projectDir) {
|
|
|
664
745
|
const registryName = pkg.registry || "default";
|
|
665
746
|
const registry = config.registries[registryName];
|
|
666
747
|
const repoDir = await ensureCloned(registry.owner, registry.repo);
|
|
667
|
-
const
|
|
668
|
-
const
|
|
669
|
-
const installDir =
|
|
748
|
+
const manifest = await readManifestFromRepo(repoDir);
|
|
749
|
+
const resolved = resolveVersionFromManifest(manifest, u.package, u.toVersion);
|
|
750
|
+
const installDir = join8(projectDir, config.install.modulesDir, u.package);
|
|
670
751
|
if (existsSync6(installDir)) {
|
|
671
752
|
await rm3(installDir, { recursive: true, force: true });
|
|
672
753
|
}
|
|
673
754
|
await mkdir5(installDir, { recursive: true });
|
|
674
|
-
await
|
|
755
|
+
await extractPackageAtCommit(repoDir, resolved.commit, resolved.path, installDir);
|
|
675
756
|
const contentHash = await hashDirectory(installDir);
|
|
676
757
|
config.packages[u.package].version = u.toVersion;
|
|
677
758
|
lock.packages[u.package] = {
|
|
678
759
|
...lock.packages[u.package],
|
|
679
760
|
version: u.toVersion,
|
|
680
|
-
tag
|
|
681
|
-
commit,
|
|
761
|
+
tag: `${u.package}/v${u.toVersion}`,
|
|
762
|
+
commit: resolved.commit,
|
|
682
763
|
contentHash
|
|
683
764
|
};
|
|
684
765
|
log.success(`Updated ${u.package} to ${u.toVersion}`);
|
|
@@ -701,8 +782,8 @@ function compareSemver(a, b) {
|
|
|
701
782
|
|
|
702
783
|
// src/lib/commands/diff.ts
|
|
703
784
|
import { existsSync as existsSync7 } from "fs";
|
|
704
|
-
import { readFile as
|
|
705
|
-
import { join as
|
|
785
|
+
import { readFile as readFile5, mkdtemp, rm as rm4 } from "fs/promises";
|
|
786
|
+
import { join as join9 } from "path";
|
|
706
787
|
import { tmpdir } from "os";
|
|
707
788
|
async function runDiff(input, projectDir) {
|
|
708
789
|
const config = await readProjectConfig(projectDir);
|
|
@@ -715,33 +796,34 @@ async function runDiff(input, projectDir) {
|
|
|
715
796
|
const pkg = config.packages[name];
|
|
716
797
|
const registry = resolveRegistryForPackage(config, name);
|
|
717
798
|
const repoDir = await ensureCloned(registry.owner, registry.repo);
|
|
799
|
+
const manifest = await readManifestFromRepo(repoDir);
|
|
718
800
|
let targetVersion;
|
|
719
801
|
if (spec.version) {
|
|
720
802
|
targetVersion = spec.version;
|
|
721
803
|
} else {
|
|
722
|
-
const
|
|
723
|
-
const versions = tags.map((t) => parseTagVersion(t, name)).filter((v) => v !== null);
|
|
804
|
+
const versions = listVersionsFromManifest(manifest, name);
|
|
724
805
|
if (versions.length === 0) {
|
|
725
|
-
throw new Error(`No versions found for ${name}`);
|
|
806
|
+
throw new Error(`No versions found for ${name} in registry manifest`);
|
|
726
807
|
}
|
|
727
|
-
|
|
808
|
+
const sorted = versions.map((v) => ({ raw: v, parts: parseSemverSimple(v) })).filter((v) => v.parts !== null).sort((a, b) => compareSemverSimple(a.parts, b.parts));
|
|
809
|
+
targetVersion = sorted[sorted.length - 1].raw;
|
|
728
810
|
}
|
|
729
811
|
if (targetVersion === pkg.version) {
|
|
730
812
|
log.info(`${name} is already at ${targetVersion}`);
|
|
731
813
|
return;
|
|
732
814
|
}
|
|
733
815
|
log.step(`Comparing ${name} ${pkg.version} \u2192 ${targetVersion}`);
|
|
734
|
-
const
|
|
735
|
-
const tmpDir = await mkdtemp(
|
|
816
|
+
const resolved = resolveVersionFromManifest(manifest, name, targetVersion);
|
|
817
|
+
const tmpDir = await mkdtemp(join9(tmpdir(), "agent-pkg-diff-"));
|
|
736
818
|
try {
|
|
737
|
-
const targetDir =
|
|
738
|
-
await
|
|
739
|
-
const currentDir =
|
|
819
|
+
const targetDir = join9(tmpDir, "target");
|
|
820
|
+
await extractPackageAtCommit(repoDir, resolved.commit, resolved.path, targetDir);
|
|
821
|
+
const currentDir = join9(projectDir, config.install.modulesDir, name);
|
|
740
822
|
if (!existsSync7(currentDir)) {
|
|
741
823
|
throw new Error(`Current package not installed at ${currentDir}. Run sync first.`);
|
|
742
824
|
}
|
|
743
|
-
const currentDsl = await loadYamlSafe(
|
|
744
|
-
const targetDsl = await loadYamlSafe(
|
|
825
|
+
const currentDsl = await loadYamlSafe(join9(currentDir, "agent-contracts.yaml"));
|
|
826
|
+
const targetDsl = await loadYamlSafe(join9(targetDir, "agent-contracts.yaml"));
|
|
745
827
|
const changes = computeDslDiff(currentDsl, targetDsl);
|
|
746
828
|
if (input.format === "json") {
|
|
747
829
|
console.log(JSON.stringify({
|
|
@@ -813,37 +895,47 @@ function printTextDiff(name, fromVersion, toVersion, changes) {
|
|
|
813
895
|
async function loadYamlSafe(path) {
|
|
814
896
|
if (!existsSync7(path)) return null;
|
|
815
897
|
const { parse } = await import("yaml");
|
|
816
|
-
const content = await
|
|
898
|
+
const content = await readFile5(path, "utf8");
|
|
817
899
|
return parse(content) || {};
|
|
818
900
|
}
|
|
901
|
+
function parseSemverSimple(v) {
|
|
902
|
+
const m = v.match(/^(\d+)\.(\d+)\.(\d+)/);
|
|
903
|
+
if (!m) return null;
|
|
904
|
+
return [+m[1], +m[2], +m[3]];
|
|
905
|
+
}
|
|
906
|
+
function compareSemverSimple(a, b) {
|
|
907
|
+
if (a[0] !== b[0]) return a[0] - b[0];
|
|
908
|
+
if (a[1] !== b[1]) return a[1] - b[1];
|
|
909
|
+
return a[2] - b[2];
|
|
910
|
+
}
|
|
819
911
|
|
|
820
912
|
// src/lib/commands/materialize.ts
|
|
821
913
|
import { existsSync as existsSync8 } from "fs";
|
|
822
|
-
import { mkdir as mkdir6, rm as rm5, readFile as
|
|
823
|
-
import { join as
|
|
914
|
+
import { mkdir as mkdir6, rm as rm5, readFile as readFile6, writeFile as writeFile5 } from "fs/promises";
|
|
915
|
+
import { join as join10 } from "path";
|
|
824
916
|
import { execFile } from "child_process";
|
|
825
917
|
async function runMaterialize(input, projectDir) {
|
|
826
918
|
const config = await readProjectConfig(projectDir);
|
|
827
|
-
const buildDir =
|
|
919
|
+
const buildDir = join10(projectDir, config.install.buildDir);
|
|
828
920
|
if (input.clean && existsSync8(buildDir)) {
|
|
829
921
|
await rm5(buildDir, { recursive: true, force: true });
|
|
830
922
|
log.step("Cleaned build directory");
|
|
831
923
|
}
|
|
832
924
|
await mkdir6(buildDir, { recursive: true });
|
|
833
925
|
log.step("Resolving agent-contracts.yaml");
|
|
834
|
-
const yamlPath =
|
|
926
|
+
const yamlPath = join10(projectDir, "agent-contracts.yaml");
|
|
835
927
|
if (!existsSync8(yamlPath)) {
|
|
836
928
|
throw new Error("agent-contracts.yaml not found in project root");
|
|
837
929
|
}
|
|
838
930
|
const { parse, stringify } = await import("yaml");
|
|
839
|
-
const rootDoc = parse(await
|
|
931
|
+
const rootDoc = parse(await readFile6(yamlPath, "utf8"));
|
|
840
932
|
const resolved = await resolveExtends(rootDoc, projectDir);
|
|
841
933
|
if (config.vars) {
|
|
842
934
|
resolved.vars = { ...resolved.vars || {}, ...config.vars };
|
|
843
935
|
}
|
|
844
936
|
const resolvedYaml = stringify(resolved, { lineWidth: 0 });
|
|
845
|
-
const resolvedPath =
|
|
846
|
-
await
|
|
937
|
+
const resolvedPath = join10(buildDir, "resolved.agent-contracts.yaml");
|
|
938
|
+
await writeFile5(resolvedPath, resolvedYaml, "utf8");
|
|
847
939
|
log.success(`Wrote ${resolvedPath}`);
|
|
848
940
|
const resolvedHash = hashString(resolvedYaml);
|
|
849
941
|
let validated = false;
|
|
@@ -864,7 +956,7 @@ async function runMaterialize(input, projectDir) {
|
|
|
864
956
|
generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
865
957
|
packages: Object.keys(config.packages)
|
|
866
958
|
};
|
|
867
|
-
await
|
|
959
|
+
await writeFile5(join10(buildDir, "package-manifest.json"), JSON.stringify(buildManifest, null, 2) + "\n", "utf8");
|
|
868
960
|
const result = {
|
|
869
961
|
resolvedDslPath: resolvedPath,
|
|
870
962
|
resolvedDslHash: resolvedHash,
|
|
@@ -881,12 +973,12 @@ async function resolveExtends(doc, baseDir) {
|
|
|
881
973
|
const { parse } = await import("yaml");
|
|
882
974
|
let merged = {};
|
|
883
975
|
for (const ext of extends_) {
|
|
884
|
-
const extPath =
|
|
976
|
+
const extPath = join10(baseDir, ext);
|
|
885
977
|
if (!existsSync8(extPath)) {
|
|
886
978
|
log.warn(`Extended file not found: ${extPath}`);
|
|
887
979
|
continue;
|
|
888
980
|
}
|
|
889
|
-
const extContent = await
|
|
981
|
+
const extContent = await readFile6(extPath, "utf8");
|
|
890
982
|
const extDoc = parse(extContent);
|
|
891
983
|
if (extDoc) {
|
|
892
984
|
merged = deepMerge(merged, extDoc);
|
|
@@ -897,12 +989,12 @@ async function resolveExtends(doc, baseDir) {
|
|
|
897
989
|
const refs = doc["$refs"];
|
|
898
990
|
if (refs) {
|
|
899
991
|
for (const ref of refs) {
|
|
900
|
-
const refPath =
|
|
992
|
+
const refPath = join10(baseDir, ref);
|
|
901
993
|
if (!existsSync8(refPath)) {
|
|
902
994
|
log.warn(`Override file not found: ${refPath}`);
|
|
903
995
|
continue;
|
|
904
996
|
}
|
|
905
|
-
const refContent = await
|
|
997
|
+
const refContent = await readFile6(refPath, "utf8");
|
|
906
998
|
const refDoc = parse(refContent);
|
|
907
999
|
if (refDoc) {
|
|
908
1000
|
merged = deepMerge(merged, refDoc);
|
|
@@ -946,7 +1038,7 @@ function runExternalTool(cmd, args, cwd2) {
|
|
|
946
1038
|
|
|
947
1039
|
// src/lib/commands/check.ts
|
|
948
1040
|
import { existsSync as existsSync9 } from "fs";
|
|
949
|
-
import { join as
|
|
1041
|
+
import { join as join11 } from "path";
|
|
950
1042
|
async function runCheck(projectDir) {
|
|
951
1043
|
const checks = [];
|
|
952
1044
|
checks.push(await checkConfigSchema(projectDir));
|
|
@@ -1030,7 +1122,7 @@ async function checkContentHashes(projectDir) {
|
|
|
1030
1122
|
try {
|
|
1031
1123
|
const lock = await readLockFile(projectDir);
|
|
1032
1124
|
for (const [name, entry] of Object.entries(lock.packages)) {
|
|
1033
|
-
const installDir =
|
|
1125
|
+
const installDir = join11(projectDir, entry.installedTo);
|
|
1034
1126
|
if (!existsSync9(installDir)) {
|
|
1035
1127
|
items.push({
|
|
1036
1128
|
name: `content_hash:${name}`,
|
|
@@ -1058,14 +1150,16 @@ async function checkContentHashes(projectDir) {
|
|
|
1058
1150
|
|
|
1059
1151
|
// src/lib/commands/publish.ts
|
|
1060
1152
|
import { existsSync as existsSync10 } from "fs";
|
|
1061
|
-
import {
|
|
1153
|
+
import { cp } from "fs/promises";
|
|
1154
|
+
import { join as join12, resolve, basename } from "path";
|
|
1062
1155
|
async function runPublish(input, projectDir) {
|
|
1063
|
-
const
|
|
1064
|
-
if (!existsSync10(
|
|
1065
|
-
throw new Error(`
|
|
1156
|
+
const sourceDir = resolve(projectDir, input.path);
|
|
1157
|
+
if (!existsSync10(sourceDir)) {
|
|
1158
|
+
throw new Error(`Source directory not found: ${input.path}`);
|
|
1066
1159
|
}
|
|
1067
|
-
const pkgJson = await readPackageJson(
|
|
1068
|
-
const manifest = await readPackageManifest(
|
|
1160
|
+
const pkgJson = await readPackageJson(sourceDir);
|
|
1161
|
+
const manifest = await readPackageManifest(sourceDir);
|
|
1162
|
+
const packageName = input.name || manifest?.id || pkgJson?.name?.split("/").pop() || basename(sourceDir);
|
|
1069
1163
|
const pkgJsonVersion = pkgJson?.version;
|
|
1070
1164
|
const manifestVersion = manifest?.version;
|
|
1071
1165
|
let version;
|
|
@@ -1076,63 +1170,108 @@ async function runPublish(input, projectDir) {
|
|
|
1076
1170
|
} else if (pkgJsonVersion) {
|
|
1077
1171
|
version = pkgJsonVersion;
|
|
1078
1172
|
} else {
|
|
1079
|
-
throw new Error(
|
|
1173
|
+
throw new Error(
|
|
1174
|
+
"Cannot determine version. Specify --version or set version in package.json / " + PACKAGE_MANIFEST_FILE
|
|
1175
|
+
);
|
|
1080
1176
|
}
|
|
1081
1177
|
if (pkgJsonVersion && manifestVersion && pkgJsonVersion !== manifestVersion) {
|
|
1082
1178
|
throw new Error(
|
|
1083
1179
|
`Version mismatch: package.json=${pkgJsonVersion}, ${PACKAGE_MANIFEST_FILE}=${manifestVersion}`
|
|
1084
1180
|
);
|
|
1085
1181
|
}
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
};
|
|
1104
|
-
log.info("Dry run \u2014 no changes made");
|
|
1105
|
-
console.log(JSON.stringify(result2, null, 2));
|
|
1106
|
-
return;
|
|
1182
|
+
let owner;
|
|
1183
|
+
let repo;
|
|
1184
|
+
if (input.registry) {
|
|
1185
|
+
({ owner, repo } = parseRegistrySpec(input.registry));
|
|
1186
|
+
} else {
|
|
1187
|
+
try {
|
|
1188
|
+
const config = await readProjectConfig(projectDir);
|
|
1189
|
+
const defaultReg = config.registries["default"];
|
|
1190
|
+
if (!defaultReg) {
|
|
1191
|
+
throw new Error("No default registry");
|
|
1192
|
+
}
|
|
1193
|
+
owner = defaultReg.owner;
|
|
1194
|
+
repo = defaultReg.repo;
|
|
1195
|
+
} catch {
|
|
1196
|
+
throw new Error(
|
|
1197
|
+
"Cannot determine target registry. Use --registry github:<owner>/<repo> or run from a project with agent-pkg.json that has a default registry."
|
|
1198
|
+
);
|
|
1107
1199
|
}
|
|
1108
1200
|
}
|
|
1109
|
-
const
|
|
1201
|
+
const description = manifest?.description || pkgJson?.description;
|
|
1202
|
+
const contentHash = await hashDirectory(sourceDir);
|
|
1203
|
+
log.step(`Publishing ${packageName}@${version} \u2192 github:${owner}/${repo}`);
|
|
1204
|
+
log.debug(`Source: ${sourceDir}`);
|
|
1205
|
+
log.debug(`Content hash: ${contentHash}`);
|
|
1110
1206
|
if (input.dryRun) {
|
|
1111
1207
|
const result2 = {
|
|
1112
1208
|
package: packageName,
|
|
1113
1209
|
version,
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
pushed: false
|
|
1210
|
+
registry: `github:${owner}/${repo}`,
|
|
1211
|
+
contentHash,
|
|
1212
|
+
pushed: false,
|
|
1213
|
+
dryRun: true
|
|
1117
1214
|
};
|
|
1118
1215
|
log.info("Dry run \u2014 no changes made");
|
|
1119
1216
|
console.log(JSON.stringify(result2, null, 2));
|
|
1120
1217
|
return;
|
|
1121
1218
|
}
|
|
1122
|
-
await
|
|
1123
|
-
|
|
1219
|
+
const workDir = await ensureWorkingCopy(owner, repo);
|
|
1220
|
+
let registryManifest;
|
|
1221
|
+
try {
|
|
1222
|
+
registryManifest = await readManifestFromDir(workDir);
|
|
1223
|
+
} catch {
|
|
1224
|
+
registryManifest = createEmptyManifest(`${owner}/${repo}`);
|
|
1225
|
+
}
|
|
1226
|
+
const existing = registryManifest.packages[packageName]?.versions[version];
|
|
1227
|
+
if (existing) {
|
|
1228
|
+
throw new Error(
|
|
1229
|
+
`Version "${version}" already exists for package "${packageName}". Bump the version before publishing.`
|
|
1230
|
+
);
|
|
1231
|
+
}
|
|
1232
|
+
const packageRoot = "packages";
|
|
1233
|
+
const packagePath = `${packageRoot}/${packageName}`;
|
|
1234
|
+
const destDir = join12(workDir, packagePath);
|
|
1235
|
+
await cp(sourceDir, destDir, {
|
|
1236
|
+
recursive: true,
|
|
1237
|
+
filter: (src) => {
|
|
1238
|
+
const name = basename(src);
|
|
1239
|
+
return name !== ".git" && name !== "node_modules" && name !== ".DS_Store";
|
|
1240
|
+
}
|
|
1241
|
+
});
|
|
1242
|
+
log.success(`Copied package files \u2192 ${packagePath}/`);
|
|
1243
|
+
const packageCommit = await commitAll(
|
|
1244
|
+
workDir,
|
|
1245
|
+
`Publish ${packageName}@${version}`
|
|
1246
|
+
);
|
|
1247
|
+
log.success(`Committed package files: ${packageCommit.slice(0, 12)}`);
|
|
1248
|
+
addVersionToManifest(
|
|
1249
|
+
registryManifest,
|
|
1250
|
+
packageName,
|
|
1251
|
+
packagePath,
|
|
1252
|
+
version,
|
|
1253
|
+
packageCommit,
|
|
1254
|
+
contentHash,
|
|
1255
|
+
description
|
|
1256
|
+
);
|
|
1257
|
+
await writeManifestToDir(workDir, registryManifest);
|
|
1258
|
+
log.success(`Updated ${REGISTRY_MANIFEST_FILE}`);
|
|
1259
|
+
const manifestCommit = await commitAll(
|
|
1260
|
+
workDir,
|
|
1261
|
+
`Register ${packageName}@${version} in manifest`
|
|
1262
|
+
);
|
|
1263
|
+
log.debug(`Manifest commit: ${manifestCommit.slice(0, 12)}`);
|
|
1124
1264
|
let pushed = false;
|
|
1125
1265
|
if (!input.noPush) {
|
|
1126
|
-
await
|
|
1266
|
+
await pushBranch(workDir);
|
|
1127
1267
|
pushed = true;
|
|
1128
|
-
log.success(
|
|
1268
|
+
log.success("Pushed to remote");
|
|
1129
1269
|
}
|
|
1130
|
-
const contentHash = await hashDirectory(packageDir);
|
|
1131
1270
|
const result = {
|
|
1132
1271
|
package: packageName,
|
|
1133
1272
|
version,
|
|
1134
|
-
|
|
1135
|
-
commit,
|
|
1273
|
+
registry: `github:${owner}/${repo}`,
|
|
1274
|
+
commit: packageCommit,
|
|
1136
1275
|
contentHash,
|
|
1137
1276
|
pushed
|
|
1138
1277
|
};
|
|
@@ -1215,8 +1354,9 @@ var handlers = {
|
|
|
1215
1354
|
await runPublish({
|
|
1216
1355
|
path,
|
|
1217
1356
|
version: opts.version,
|
|
1357
|
+
registry: opts.registry,
|
|
1358
|
+
name: opts.name,
|
|
1218
1359
|
dryRun: opts.dryRun,
|
|
1219
|
-
tagOnly: opts.tagOnly,
|
|
1220
1360
|
noPush: opts.noPush
|
|
1221
1361
|
}, cwd).catch(handleError);
|
|
1222
1362
|
}
|