@zookanalytics/agent-env 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +662 -375
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -42,7 +42,7 @@ import React3 from "react";
|
|
|
42
42
|
// package.json
|
|
43
43
|
var package_default = {
|
|
44
44
|
name: "@zookanalytics/agent-env",
|
|
45
|
-
version: "0.
|
|
45
|
+
version: "0.6.0",
|
|
46
46
|
description: "CLI for creating isolated, AI-ready development environments",
|
|
47
47
|
type: "module",
|
|
48
48
|
bin: {
|
|
@@ -103,9 +103,9 @@ var package_default = {
|
|
|
103
103
|
import { Command } from "commander";
|
|
104
104
|
|
|
105
105
|
// src/lib/attach-instance.ts
|
|
106
|
-
import { appendFile as appendFile4, mkdir as mkdir6, readdir as readdir5, readFile as
|
|
106
|
+
import { appendFile as appendFile4, mkdir as mkdir6, readdir as readdir5, readFile as readFile8, rename as rename5, stat as stat5, writeFile as writeFile7 } from "fs/promises";
|
|
107
107
|
import { homedir as homedir4 } from "os";
|
|
108
|
-
import { join as
|
|
108
|
+
import { join as join7 } from "path";
|
|
109
109
|
|
|
110
110
|
// src/lib/container.ts
|
|
111
111
|
var DEVCONTAINER_UP_TIMEOUT = 3e5;
|
|
@@ -406,28 +406,30 @@ function createContainerLifecycle(executor = createExecutor()) {
|
|
|
406
406
|
|
|
407
407
|
// src/lib/create-instance.ts
|
|
408
408
|
import {
|
|
409
|
+
access as access3,
|
|
409
410
|
appendFile as appendFile3,
|
|
410
411
|
cp as cp2,
|
|
411
412
|
mkdir as mkdir5,
|
|
412
413
|
readdir as readdir4,
|
|
413
|
-
readFile as
|
|
414
|
-
rename as
|
|
414
|
+
readFile as readFile7,
|
|
415
|
+
rename as rename4,
|
|
415
416
|
rm as rm2,
|
|
416
417
|
stat as stat4,
|
|
417
|
-
writeFile as
|
|
418
|
+
writeFile as writeFile6
|
|
418
419
|
} from "fs/promises";
|
|
419
420
|
import { homedir as homedir3 } from "os";
|
|
420
|
-
import { dirname as dirname2, join as
|
|
421
|
+
import { dirname as dirname2, join as join6 } from "path";
|
|
421
422
|
|
|
422
|
-
// src/lib/devcontainer.ts
|
|
423
|
-
import { parse as
|
|
424
|
-
import {
|
|
425
|
-
import {
|
|
426
|
-
import {
|
|
423
|
+
// src/lib/devcontainer-merge.ts
|
|
424
|
+
import { parse as parseJsonc2 } from "jsonc-parser";
|
|
425
|
+
import { constants } from "fs";
|
|
426
|
+
import { access as access2, readFile as readFile2, rename, writeFile as writeFile2 } from "fs/promises";
|
|
427
|
+
import { createRequire } from "module";
|
|
428
|
+
import { join as join2 } from "path";
|
|
427
429
|
|
|
428
430
|
// src/lib/container-env.ts
|
|
429
|
-
var CONTAINER_STATE_PATH = "/etc/agent-env/state.json";
|
|
430
|
-
var CONTAINER_AGENT_ENV_DIR = "/etc/agent-env";
|
|
431
|
+
var CONTAINER_STATE_PATH = process.env.AGENT_ENV_STATE_PATH || "/etc/agent-env/state.json";
|
|
432
|
+
var CONTAINER_AGENT_ENV_DIR = process.env.AGENT_ENV_CONTAINER_DIR || "/etc/agent-env";
|
|
431
433
|
var defaultContainerEnvDeps = {
|
|
432
434
|
getEnv: (key) => process.env[key]
|
|
433
435
|
};
|
|
@@ -435,6 +437,13 @@ function isInsideContainer(deps = defaultContainerEnvDeps) {
|
|
|
435
437
|
return deps.getEnv("AGENT_ENV_CONTAINER") === "true";
|
|
436
438
|
}
|
|
437
439
|
|
|
440
|
+
// src/lib/devcontainer.ts
|
|
441
|
+
import { parse as parseJsonc } from "jsonc-parser";
|
|
442
|
+
import { accessSync } from "fs";
|
|
443
|
+
import { access, cp, mkdir, readdir, readFile, stat, writeFile } from "fs/promises";
|
|
444
|
+
import { dirname, join } from "path";
|
|
445
|
+
import { fileURLToPath } from "url";
|
|
446
|
+
|
|
438
447
|
// src/lib/types.ts
|
|
439
448
|
var AGENT_ENV_DIR = ".agent-env";
|
|
440
449
|
var WORKSPACES_DIR = "workspaces";
|
|
@@ -457,7 +466,8 @@ function createFallbackState(workspaceName) {
|
|
|
457
466
|
createdAt: "unknown",
|
|
458
467
|
lastAttached: "unknown",
|
|
459
468
|
purpose: null,
|
|
460
|
-
containerName: `${CONTAINER_PREFIX}${workspaceName}
|
|
469
|
+
containerName: `${CONTAINER_PREFIX}${workspaceName}`,
|
|
470
|
+
repoConfigDetected: false
|
|
461
471
|
};
|
|
462
472
|
}
|
|
463
473
|
|
|
@@ -466,50 +476,34 @@ var defaultFsDeps = { access, cp, mkdir, readdir, readFile, stat, writeFile };
|
|
|
466
476
|
var DEVCONTAINER_DIR = ".devcontainer";
|
|
467
477
|
var DEVCONTAINER_JSON = "devcontainer.json";
|
|
468
478
|
var DOT_DEVCONTAINER_JSON = ".devcontainer.json";
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
if (fileStats.isFile()) {
|
|
490
|
-
return true;
|
|
491
|
-
}
|
|
492
|
-
} catch {
|
|
493
|
-
}
|
|
494
|
-
try {
|
|
495
|
-
const dotFileStats = await deps.stat(join(workspacePath, DOT_DEVCONTAINER_JSON));
|
|
496
|
-
if (dotFileStats.isFile()) {
|
|
497
|
-
return true;
|
|
479
|
+
var cachedPackageRoot;
|
|
480
|
+
function getPackageRoot(checkAccess = accessSync) {
|
|
481
|
+
if (cachedPackageRoot) return cachedPackageRoot;
|
|
482
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
483
|
+
const startDir = dir;
|
|
484
|
+
for (; ; ) {
|
|
485
|
+
try {
|
|
486
|
+
checkAccess(join(dir, "package.json"));
|
|
487
|
+
cachedPackageRoot = dir;
|
|
488
|
+
return dir;
|
|
489
|
+
} catch (err) {
|
|
490
|
+
const code = err?.code;
|
|
491
|
+
if (code === "EACCES" || code === "EPERM") {
|
|
492
|
+
throw new Error(`Permission denied accessing ${join(dir, "package.json")}`);
|
|
493
|
+
}
|
|
494
|
+
const parent = dirname(dir);
|
|
495
|
+
if (parent === dir) {
|
|
496
|
+
throw new Error(`Could not find package.json in ${startDir} or any parent directory`);
|
|
497
|
+
}
|
|
498
|
+
dir = parent;
|
|
498
499
|
}
|
|
499
|
-
} catch {
|
|
500
500
|
}
|
|
501
|
-
return false;
|
|
502
501
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
throw new Error(`Baseline config not found at ${baselinePath}. Package may be corrupted.`);
|
|
509
|
-
}
|
|
510
|
-
const targetDir = join(workspacePath, AGENT_ENV_DIR);
|
|
511
|
-
await deps.mkdir(targetDir, { recursive: true });
|
|
512
|
-
await deps.cp(baselinePath, targetDir, { recursive: true });
|
|
502
|
+
function getBaselineConfigPath() {
|
|
503
|
+
return join(getPackageRoot(), "config", "baseline");
|
|
504
|
+
}
|
|
505
|
+
function getTemplatesPath() {
|
|
506
|
+
return join(getPackageRoot(), "config", "templates");
|
|
513
507
|
}
|
|
514
508
|
async function copyStatusBarTemplate(workspacePath, deps = defaultFsDeps) {
|
|
515
509
|
const templatesPath = getTemplatesPath();
|
|
@@ -524,58 +518,15 @@ async function copyStatusBarTemplate(workspacePath, deps = defaultFsDeps) {
|
|
|
524
518
|
await deps.mkdir(targetDir, { recursive: true });
|
|
525
519
|
await deps.cp(srcFile, join(targetDir, "statusBar.template.json"));
|
|
526
520
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
function applyContainerEnvPatch(config, envVars) {
|
|
535
|
-
const existing = typeof config.containerEnv === "object" && config.containerEnv !== null ? config.containerEnv : {};
|
|
536
|
-
config.containerEnv = { ...existing, ...envVars };
|
|
537
|
-
return config;
|
|
538
|
-
}
|
|
539
|
-
function applyVscodeSettingsPatch(config) {
|
|
540
|
-
if (typeof config.customizations !== "object" || config.customizations === null) {
|
|
541
|
-
config.customizations = {};
|
|
542
|
-
}
|
|
543
|
-
const customizations = config.customizations;
|
|
544
|
-
if (typeof customizations.vscode !== "object" || customizations.vscode === null) {
|
|
545
|
-
customizations.vscode = {};
|
|
546
|
-
}
|
|
547
|
-
const vscode = customizations.vscode;
|
|
548
|
-
if (typeof vscode.settings !== "object" || vscode.settings === null) {
|
|
549
|
-
vscode.settings = {};
|
|
550
|
-
}
|
|
551
|
-
const settings = vscode.settings;
|
|
552
|
-
const existingFilewatcherCommands = Array.isArray(settings["filewatcher.commands"]) ? settings["filewatcher.commands"] : [];
|
|
553
|
-
const statusBarWatcher = {
|
|
554
|
-
match: `${STATUS_BAR_JSON_FILENAME}$`,
|
|
555
|
-
event: "onFolderChange",
|
|
556
|
-
vscodeTask: "betterStatusBar.refreshButtons"
|
|
557
|
-
};
|
|
558
|
-
const hasStatusBarWatcher = existingFilewatcherCommands.some((cmd) => {
|
|
559
|
-
if (typeof cmd !== "object" || cmd === null) return false;
|
|
560
|
-
const c = cmd;
|
|
561
|
-
return c.match === statusBarWatcher.match && c.event === statusBarWatcher.event && c.vscodeTask === statusBarWatcher.vscodeTask;
|
|
521
|
+
async function copyManagedAssets(workspacePath, deps = defaultFsDeps) {
|
|
522
|
+
const baselinePath = getBaselineConfigPath();
|
|
523
|
+
const targetDir = join(workspacePath, AGENT_ENV_DIR);
|
|
524
|
+
await deps.mkdir(targetDir, { recursive: true });
|
|
525
|
+
await deps.cp(baselinePath, targetDir, {
|
|
526
|
+
recursive: true,
|
|
527
|
+
filter: (source) => !source.endsWith("devcontainer.json")
|
|
562
528
|
});
|
|
563
|
-
|
|
564
|
-
vscode.settings = {
|
|
565
|
-
...settings,
|
|
566
|
-
"betterStatusBar.configurationFile": `${CONTAINER_AGENT_ENV_DIR}/${STATUS_BAR_JSON_FILENAME}`,
|
|
567
|
-
"filewatcher.commands": mergedFilewatcherCommands
|
|
568
|
-
};
|
|
569
|
-
return config;
|
|
570
|
-
}
|
|
571
|
-
async function applyBaselinePatches(workspacePath, containerName, envVars, deps, configDir) {
|
|
572
|
-
const configPath = join(workspacePath, configDir, "devcontainer.json");
|
|
573
|
-
const content = await deps.readFile(configPath, "utf-8");
|
|
574
|
-
let config = JSON.parse(content);
|
|
575
|
-
config = applyContainerNamePatch(config, containerName);
|
|
576
|
-
config = applyContainerEnvPatch(config, envVars);
|
|
577
|
-
config = applyVscodeSettingsPatch(config);
|
|
578
|
-
await deps.writeFile(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
529
|
+
await copyStatusBarTemplate(workspacePath, deps);
|
|
579
530
|
}
|
|
580
531
|
async function resolveDockerfilePath(workspacePath, deps) {
|
|
581
532
|
const candidates = [
|
|
@@ -647,22 +598,399 @@ function parseDockerfileImages(content, logger) {
|
|
|
647
598
|
return [...images];
|
|
648
599
|
}
|
|
649
600
|
|
|
601
|
+
// src/lib/devcontainer-merge.ts
|
|
602
|
+
var defaultMergeDeps = { readFile: readFile2, writeFile: writeFile2, rename, access: access2 };
|
|
603
|
+
var STATUS_BAR_JSON_FILENAME = "statusBar.json";
|
|
604
|
+
var LABEL_LIFECYCLE_CMDS = {
|
|
605
|
+
postCreateCommand: "/usr/local/bin/post-create.sh",
|
|
606
|
+
postStartCommand: "sudo /usr/local/bin/start-sshd.sh; tmux new-session -d -s main 2>/dev/null || true"
|
|
607
|
+
};
|
|
608
|
+
var COMPOSABLE_LIFECYCLE_CMDS = [
|
|
609
|
+
"postCreateCommand",
|
|
610
|
+
"postStartCommand",
|
|
611
|
+
"postAttachCommand",
|
|
612
|
+
"onCreateCommand",
|
|
613
|
+
"updateContentCommand"
|
|
614
|
+
];
|
|
615
|
+
var AUTO_GENERATED_HEADER = "// AUTO-GENERATED by agent-env";
|
|
616
|
+
async function readRepoConfig(workspacePath, deps = defaultMergeDeps, logger) {
|
|
617
|
+
const candidates = [
|
|
618
|
+
join2(workspacePath, ".devcontainer", "devcontainer.json"),
|
|
619
|
+
join2(workspacePath, "devcontainer.json"),
|
|
620
|
+
join2(workspacePath, ".devcontainer.json")
|
|
621
|
+
];
|
|
622
|
+
for (const candidate of candidates) {
|
|
623
|
+
let content;
|
|
624
|
+
try {
|
|
625
|
+
await deps.access(candidate, constants.F_OK);
|
|
626
|
+
content = await deps.readFile(candidate, "utf-8");
|
|
627
|
+
} catch (err) {
|
|
628
|
+
const code = err?.code;
|
|
629
|
+
if (code === "ENOENT") {
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
throw err;
|
|
633
|
+
}
|
|
634
|
+
if (content.startsWith(AUTO_GENERATED_HEADER)) {
|
|
635
|
+
logger?.warn(
|
|
636
|
+
`Skipping auto-generated config at ${candidate} \u2014 this appears to be agent-env's own output, not a repo config`
|
|
637
|
+
);
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
const errors = [];
|
|
641
|
+
const parsed = parseJsonc2(content, errors, { allowTrailingComma: true });
|
|
642
|
+
if (errors.length > 0) {
|
|
643
|
+
throw new Error(`Failed to parse devcontainer config at ${candidate}: Invalid JSONC`);
|
|
644
|
+
}
|
|
645
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
646
|
+
throw new Error(
|
|
647
|
+
`Failed to parse devcontainer config at ${candidate}: Expected a JSON object`
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
return parsed;
|
|
651
|
+
}
|
|
652
|
+
return void 0;
|
|
653
|
+
}
|
|
654
|
+
function validateRepoConfig(config, logger) {
|
|
655
|
+
const rejectedProps = ["build", "dockerFile", "dockerfile", "dockerComposeFile"];
|
|
656
|
+
for (const prop of rejectedProps) {
|
|
657
|
+
if (prop in config && config[prop] !== void 0) {
|
|
658
|
+
throw new Error(
|
|
659
|
+
`agent-env requires the managed image. Remove '${prop}' from .devcontainer/devcontainer.json to use agent-env.`
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
if ("image" in config && config.image !== void 0) {
|
|
664
|
+
const repoImage = String(config.image);
|
|
665
|
+
logger?.warn(
|
|
666
|
+
`Repo config specifies image '${repoImage}' which will be overridden by agent-env managed image.`
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
function composeName(instanceName, repoName) {
|
|
671
|
+
if (typeof repoName === "string" && repoName.trim()) {
|
|
672
|
+
return `${repoName} - ${instanceName}`;
|
|
673
|
+
}
|
|
674
|
+
return instanceName;
|
|
675
|
+
}
|
|
676
|
+
function composeLifecycle(managedCmd, repoCmd) {
|
|
677
|
+
if (repoCmd === void 0 || repoCmd === null) {
|
|
678
|
+
return managedCmd;
|
|
679
|
+
}
|
|
680
|
+
if (typeof repoCmd === "string") {
|
|
681
|
+
return { "agent-env": managedCmd, repo: repoCmd };
|
|
682
|
+
}
|
|
683
|
+
if (Array.isArray(repoCmd)) {
|
|
684
|
+
return { "agent-env": managedCmd, repo: repoCmd };
|
|
685
|
+
}
|
|
686
|
+
if (typeof repoCmd === "object") {
|
|
687
|
+
return { "agent-env": managedCmd, ...repoCmd };
|
|
688
|
+
}
|
|
689
|
+
return managedCmd;
|
|
690
|
+
}
|
|
691
|
+
function composeAllLifecycle(managedLifecycleCmds, repoLifecycleCmds) {
|
|
692
|
+
const result = {};
|
|
693
|
+
for (const cmdName of COMPOSABLE_LIFECYCLE_CMDS) {
|
|
694
|
+
const managedCmd = managedLifecycleCmds[cmdName];
|
|
695
|
+
const repoCmd = repoLifecycleCmds[cmdName];
|
|
696
|
+
if (managedCmd && repoCmd !== void 0 && repoCmd !== null) {
|
|
697
|
+
result[cmdName] = composeLifecycle(managedCmd, repoCmd);
|
|
698
|
+
} else if (managedCmd && (repoCmd === void 0 || repoCmd === null)) {
|
|
699
|
+
} else if (!managedCmd && repoCmd !== void 0 && repoCmd !== null) {
|
|
700
|
+
if (typeof repoCmd === "string") {
|
|
701
|
+
result[cmdName] = { repo: repoCmd };
|
|
702
|
+
} else if (Array.isArray(repoCmd)) {
|
|
703
|
+
result[cmdName] = { repo: repoCmd };
|
|
704
|
+
} else if (typeof repoCmd === "object") {
|
|
705
|
+
result[cmdName] = repoCmd;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
return result;
|
|
710
|
+
}
|
|
711
|
+
function mergeRunArgs(managedArgs, repoArgs) {
|
|
712
|
+
if (!Array.isArray(repoArgs)) {
|
|
713
|
+
return [...managedArgs];
|
|
714
|
+
}
|
|
715
|
+
const filtered = [];
|
|
716
|
+
let i = 0;
|
|
717
|
+
while (i < repoArgs.length) {
|
|
718
|
+
const arg = String(repoArgs[i]);
|
|
719
|
+
if (arg.startsWith("--name=")) {
|
|
720
|
+
i++;
|
|
721
|
+
continue;
|
|
722
|
+
}
|
|
723
|
+
if (arg === "--name") {
|
|
724
|
+
i += i + 1 < repoArgs.length ? 2 : 1;
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
filtered.push(arg);
|
|
728
|
+
i++;
|
|
729
|
+
}
|
|
730
|
+
return [...managedArgs, ...filtered];
|
|
731
|
+
}
|
|
732
|
+
function extractMountTarget(mount) {
|
|
733
|
+
if (typeof mount === "string") {
|
|
734
|
+
const parts = mount.split(",");
|
|
735
|
+
for (const part of parts) {
|
|
736
|
+
const trimmed = part.trim();
|
|
737
|
+
if (trimmed.startsWith("target=") || trimmed.startsWith("destination=")) {
|
|
738
|
+
return trimmed.split("=").slice(1).join("=");
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return void 0;
|
|
742
|
+
}
|
|
743
|
+
if (typeof mount === "object" && mount !== null) {
|
|
744
|
+
const obj = mount;
|
|
745
|
+
if (typeof obj.target === "string") return obj.target;
|
|
746
|
+
if (typeof obj.destination === "string") return obj.destination;
|
|
747
|
+
}
|
|
748
|
+
return void 0;
|
|
749
|
+
}
|
|
750
|
+
function mergeMounts(managedMounts, repoMounts) {
|
|
751
|
+
if (!Array.isArray(repoMounts)) {
|
|
752
|
+
return [...managedMounts];
|
|
753
|
+
}
|
|
754
|
+
const managedTargets = /* @__PURE__ */ new Set();
|
|
755
|
+
for (const mount of managedMounts) {
|
|
756
|
+
const target = extractMountTarget(mount);
|
|
757
|
+
if (target) managedTargets.add(target);
|
|
758
|
+
}
|
|
759
|
+
const filteredRepo = [];
|
|
760
|
+
for (const mount of repoMounts) {
|
|
761
|
+
const target = extractMountTarget(mount);
|
|
762
|
+
if (target && managedTargets.has(target)) {
|
|
763
|
+
continue;
|
|
764
|
+
}
|
|
765
|
+
if (typeof mount === "string") {
|
|
766
|
+
filteredRepo.push(mount);
|
|
767
|
+
} else if (typeof mount === "object" && mount !== null) {
|
|
768
|
+
const obj = mount;
|
|
769
|
+
const parts = [];
|
|
770
|
+
if (obj.source) parts.push(`source=${obj.source}`);
|
|
771
|
+
if (obj.target) parts.push(`target=${obj.target}`);
|
|
772
|
+
else if (obj.destination) parts.push(`target=${obj.destination}`);
|
|
773
|
+
if (obj.type) parts.push(`type=${obj.type}`);
|
|
774
|
+
filteredRepo.push(parts.join(","));
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
return [...managedMounts, ...filteredRepo];
|
|
778
|
+
}
|
|
779
|
+
function deepMergeCustomizations(managed, repo) {
|
|
780
|
+
if (!repo || typeof repo !== "object") {
|
|
781
|
+
return {
|
|
782
|
+
vscode: {
|
|
783
|
+
settings: { ...managed.vscode.settings },
|
|
784
|
+
extensions: [...managed.vscode.extensions]
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
const repoCustom = repo;
|
|
789
|
+
const repoVscode = typeof repoCustom.vscode === "object" && repoCustom.vscode !== null ? repoCustom.vscode : {};
|
|
790
|
+
const repoExtensions = Array.isArray(repoVscode.extensions) ? repoVscode.extensions : [];
|
|
791
|
+
const allExtensions = [...managed.vscode.extensions, ...repoExtensions];
|
|
792
|
+
const seen = /* @__PURE__ */ new Set();
|
|
793
|
+
const dedupedExtensions = [];
|
|
794
|
+
for (const ext of allExtensions) {
|
|
795
|
+
const lower = ext.toLowerCase();
|
|
796
|
+
if (!seen.has(lower)) {
|
|
797
|
+
seen.add(lower);
|
|
798
|
+
dedupedExtensions.push(ext);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
const repoSettings = typeof repoVscode.settings === "object" && repoVscode.settings !== null ? repoVscode.settings : {};
|
|
802
|
+
const repoFilewatcherCommands = Array.isArray(repoSettings["filewatcher.commands"]) ? repoSettings["filewatcher.commands"] : [];
|
|
803
|
+
const managedFilewatcherCommands = Array.isArray(managed.vscode.settings["filewatcher.commands"]) ? managed.vscode.settings["filewatcher.commands"] : [];
|
|
804
|
+
const mergedFilewatcherCommands = [...repoFilewatcherCommands];
|
|
805
|
+
for (const managedCmd of managedFilewatcherCommands) {
|
|
806
|
+
if (typeof managedCmd !== "object" || managedCmd === null) continue;
|
|
807
|
+
const mCmd = managedCmd;
|
|
808
|
+
const alreadyExists = mergedFilewatcherCommands.some((existing) => {
|
|
809
|
+
if (typeof existing !== "object" || existing === null) return false;
|
|
810
|
+
return existing.match === mCmd.match;
|
|
811
|
+
});
|
|
812
|
+
if (!alreadyExists) {
|
|
813
|
+
mergedFilewatcherCommands.push(managedCmd);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
const mergedSettings = {
|
|
817
|
+
...repoSettings,
|
|
818
|
+
...managed.vscode.settings
|
|
819
|
+
};
|
|
820
|
+
if (mergedFilewatcherCommands.length > 0) {
|
|
821
|
+
mergedSettings["filewatcher.commands"] = mergedFilewatcherCommands;
|
|
822
|
+
}
|
|
823
|
+
const result = {};
|
|
824
|
+
for (const [key, value] of Object.entries(repoCustom)) {
|
|
825
|
+
if (key !== "vscode") {
|
|
826
|
+
result[key] = value;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
result.vscode = {
|
|
830
|
+
...repoVscode,
|
|
831
|
+
settings: mergedSettings,
|
|
832
|
+
extensions: dedupedExtensions
|
|
833
|
+
};
|
|
834
|
+
return result;
|
|
835
|
+
}
|
|
836
|
+
function buildManagedOnly(managed) {
|
|
837
|
+
return {
|
|
838
|
+
image: managed.image,
|
|
839
|
+
name: managed.instanceName,
|
|
840
|
+
initializeCommand: managed.initializeCmd,
|
|
841
|
+
runArgs: [...managed.runArgs],
|
|
842
|
+
containerEnv: { ...managed.containerEnv },
|
|
843
|
+
mounts: [...managed.mounts],
|
|
844
|
+
customizations: deepMergeCustomizations(managed.customizations)
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
function mergeDevcontainerConfigs(managed, repoConfig) {
|
|
848
|
+
if (!repoConfig) return buildManagedOnly(managed);
|
|
849
|
+
const {
|
|
850
|
+
name,
|
|
851
|
+
image: _image,
|
|
852
|
+
initializeCommand,
|
|
853
|
+
postCreateCommand,
|
|
854
|
+
postStartCommand,
|
|
855
|
+
postAttachCommand,
|
|
856
|
+
onCreateCommand,
|
|
857
|
+
updateContentCommand,
|
|
858
|
+
runArgs,
|
|
859
|
+
containerEnv,
|
|
860
|
+
customizations,
|
|
861
|
+
mounts,
|
|
862
|
+
...repoRest
|
|
863
|
+
} = repoConfig;
|
|
864
|
+
return {
|
|
865
|
+
...repoRest,
|
|
866
|
+
image: managed.image,
|
|
867
|
+
name: composeName(managed.instanceName, name),
|
|
868
|
+
initializeCommand: composeLifecycle(managed.initializeCmd, initializeCommand),
|
|
869
|
+
...composeAllLifecycle(managed.lifecycleCmds, {
|
|
870
|
+
postCreateCommand,
|
|
871
|
+
postStartCommand,
|
|
872
|
+
postAttachCommand,
|
|
873
|
+
onCreateCommand,
|
|
874
|
+
updateContentCommand
|
|
875
|
+
}),
|
|
876
|
+
runArgs: mergeRunArgs(managed.runArgs, runArgs),
|
|
877
|
+
containerEnv: {
|
|
878
|
+
...typeof containerEnv === "object" && containerEnv !== null ? Object.fromEntries(
|
|
879
|
+
Object.entries(containerEnv).map(([k, v]) => [k, String(v)])
|
|
880
|
+
) : {},
|
|
881
|
+
...managed.containerEnv
|
|
882
|
+
},
|
|
883
|
+
mounts: mergeMounts(managed.mounts, mounts),
|
|
884
|
+
customizations: deepMergeCustomizations(managed.customizations, customizations)
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
async function loadManagedDefaults(deps = defaultMergeDeps) {
|
|
888
|
+
const baselinePath = getBaselineConfigPath();
|
|
889
|
+
const configPath = join2(baselinePath, "devcontainer.json");
|
|
890
|
+
let content;
|
|
891
|
+
try {
|
|
892
|
+
content = await deps.readFile(configPath, "utf-8");
|
|
893
|
+
} catch {
|
|
894
|
+
throw new Error(
|
|
895
|
+
`Failed to load managed defaults from baseline config: ${configPath}. Ensure agent-env package is intact.`
|
|
896
|
+
);
|
|
897
|
+
}
|
|
898
|
+
const errors = [];
|
|
899
|
+
const config = parseJsonc2(content, errors, { allowTrailingComma: true });
|
|
900
|
+
if (errors.length > 0) {
|
|
901
|
+
throw new Error(
|
|
902
|
+
`Failed to load managed defaults from baseline config: ${configPath}. Ensure agent-env package is intact.`
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
if (typeof config.image !== "string") {
|
|
906
|
+
throw new Error(
|
|
907
|
+
`Failed to load managed defaults from baseline config: ${configPath}. Missing 'image' field.`
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
if (typeof config.initializeCommand !== "string") {
|
|
911
|
+
throw new Error(
|
|
912
|
+
`Failed to load managed defaults from baseline config: ${configPath}. Missing 'initializeCommand' field.`
|
|
913
|
+
);
|
|
914
|
+
}
|
|
915
|
+
const baseContainerEnv = {};
|
|
916
|
+
if (typeof config.containerEnv === "object" && config.containerEnv !== null) {
|
|
917
|
+
const env = config.containerEnv;
|
|
918
|
+
if (typeof env.AGENT_ENV_CONTAINER === "string") {
|
|
919
|
+
baseContainerEnv.AGENT_ENV_CONTAINER = env.AGENT_ENV_CONTAINER;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
return {
|
|
923
|
+
image: config.image,
|
|
924
|
+
initializeCmd: config.initializeCommand,
|
|
925
|
+
baseContainerEnv
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
function getPackageVersion() {
|
|
929
|
+
try {
|
|
930
|
+
const req = createRequire(import.meta.url);
|
|
931
|
+
const pkg = req("../../package.json");
|
|
932
|
+
return pkg.version;
|
|
933
|
+
} catch {
|
|
934
|
+
return "unknown";
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
async function writeGeneratedConfig(outputPath, config, deps = defaultMergeDeps) {
|
|
938
|
+
const version2 = getPackageVersion();
|
|
939
|
+
const header = `${AUTO_GENERATED_HEADER} v${version2}. Do not edit.
|
|
940
|
+
`;
|
|
941
|
+
const json = JSON.stringify(config, null, 2);
|
|
942
|
+
const content = `${header}${json}
|
|
943
|
+
`;
|
|
944
|
+
const tmpPath = `${outputPath}.tmp`;
|
|
945
|
+
await deps.writeFile(tmpPath, content, "utf-8");
|
|
946
|
+
await deps.rename(tmpPath, outputPath);
|
|
947
|
+
}
|
|
948
|
+
function buildManagedConfig(defaults, params) {
|
|
949
|
+
const statusBarWatcher = {
|
|
950
|
+
match: `${STATUS_BAR_JSON_FILENAME}$`,
|
|
951
|
+
event: "onFolderChange",
|
|
952
|
+
vscodeTask: "betterStatusBar.refreshButtons"
|
|
953
|
+
};
|
|
954
|
+
return {
|
|
955
|
+
image: defaults.image,
|
|
956
|
+
instanceName: params.instanceName,
|
|
957
|
+
containerName: params.containerName,
|
|
958
|
+
initializeCmd: defaults.initializeCmd,
|
|
959
|
+
lifecycleCmds: { ...LABEL_LIFECYCLE_CMDS },
|
|
960
|
+
runArgs: [`--name=${params.containerName}`],
|
|
961
|
+
containerEnv: {
|
|
962
|
+
...defaults.baseContainerEnv,
|
|
963
|
+
AGENT_ENV_INSTANCE: params.instanceName,
|
|
964
|
+
AGENT_ENV_REPO: params.repoSlug,
|
|
965
|
+
AGENT_ENV_PURPOSE: params.purpose
|
|
966
|
+
},
|
|
967
|
+
mounts: [],
|
|
968
|
+
customizations: {
|
|
969
|
+
vscode: {
|
|
970
|
+
settings: {
|
|
971
|
+
"betterStatusBar.configurationFile": `${CONTAINER_AGENT_ENV_DIR}/${STATUS_BAR_JSON_FILENAME}`,
|
|
972
|
+
"filewatcher.commands": [statusBarWatcher]
|
|
973
|
+
},
|
|
974
|
+
extensions: []
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
|
|
650
980
|
// src/lib/purpose-instance.ts
|
|
651
|
-
import { appendFile as appendFile2, mkdir as mkdir4, readdir as readdir3, readFile as
|
|
981
|
+
import { appendFile as appendFile2, mkdir as mkdir4, readdir as readdir3, readFile as readFile6, rename as rename3, stat as stat3, writeFile as writeFile5 } from "fs/promises";
|
|
652
982
|
import { homedir as homedir2 } from "os";
|
|
653
983
|
|
|
654
984
|
// src/lib/state.ts
|
|
655
|
-
import { appendFile, mkdir as mkdir2, readFile as
|
|
656
|
-
import { join as
|
|
657
|
-
var defaultStateFsDeps = { readFile:
|
|
985
|
+
import { appendFile, mkdir as mkdir2, readFile as readFile3, rename as rename2, writeFile as writeFile3 } from "fs/promises";
|
|
986
|
+
import { join as join3 } from "path";
|
|
987
|
+
var defaultStateFsDeps = { readFile: readFile3, writeFile: writeFile3, rename: rename2, mkdir: mkdir2, appendFile };
|
|
658
988
|
async function readState(wsPath, deps = defaultStateFsDeps) {
|
|
659
989
|
try {
|
|
660
990
|
const content = await deps.readFile(wsPath.stateFile, "utf-8");
|
|
661
991
|
const parsed = JSON.parse(content);
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
}
|
|
665
|
-
return parsed;
|
|
992
|
+
const state = isValidState(parsed) ? parsed : migrateOldState(parsed, wsPath.name) ?? createFallbackState(wsPath.name);
|
|
993
|
+
return migrateConfigSource(state);
|
|
666
994
|
} catch (err) {
|
|
667
995
|
if (err instanceof SyntaxError || err.code === "ENOENT") {
|
|
668
996
|
return createFallbackState(wsPath.name);
|
|
@@ -671,14 +999,16 @@ async function readState(wsPath, deps = defaultStateFsDeps) {
|
|
|
671
999
|
}
|
|
672
1000
|
}
|
|
673
1001
|
async function writeStateAtomic(wsPath, state, deps = defaultStateFsDeps) {
|
|
674
|
-
const tmpPath =
|
|
1002
|
+
const tmpPath = join3(wsPath.agentEnvDir, STATE_FILE_TMP);
|
|
675
1003
|
await deps.mkdir(wsPath.agentEnvDir, { recursive: true });
|
|
676
1004
|
const content = JSON.stringify(state, null, 2) + "\n";
|
|
677
1005
|
await deps.writeFile(tmpPath, content, "utf-8");
|
|
678
1006
|
await deps.rename(tmpPath, wsPath.stateFile);
|
|
679
1007
|
}
|
|
680
1008
|
function createInitialState(instance, repoSlug, repoUrl, options) {
|
|
681
|
-
const { containerName,
|
|
1009
|
+
const { containerName, repoConfigDetected, purpose } = options ?? {
|
|
1010
|
+
repoConfigDetected: false
|
|
1011
|
+
};
|
|
682
1012
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
683
1013
|
const workspaceName = `${repoSlug}-${instance}`;
|
|
684
1014
|
return {
|
|
@@ -689,7 +1019,7 @@ function createInitialState(instance, repoSlug, repoUrl, options) {
|
|
|
689
1019
|
lastAttached: now,
|
|
690
1020
|
purpose: purpose ?? null,
|
|
691
1021
|
containerName: containerName ?? `${CONTAINER_PREFIX}${workspaceName}`,
|
|
692
|
-
|
|
1022
|
+
repoConfigDetected
|
|
693
1023
|
};
|
|
694
1024
|
}
|
|
695
1025
|
function extractRepoName(url) {
|
|
@@ -726,9 +1056,17 @@ function isValidState(value) {
|
|
|
726
1056
|
const obj = value;
|
|
727
1057
|
return typeof obj.instance === "string" && typeof obj.repoSlug === "string" && typeof obj.repoUrl === "string" && typeof obj.createdAt === "string" && typeof obj.lastAttached === "string" && (obj.purpose === void 0 || obj.purpose === null || typeof obj.purpose === "string") && typeof obj.containerName === "string";
|
|
728
1058
|
}
|
|
1059
|
+
function migrateConfigSource(state) {
|
|
1060
|
+
const { configSource, repoConfigDetected, ...rest } = state;
|
|
1061
|
+
const normalizedRepoConfigDetected = repoConfigDetected !== void 0 ? repoConfigDetected : configSource === "repo";
|
|
1062
|
+
return {
|
|
1063
|
+
...rest,
|
|
1064
|
+
repoConfigDetected: normalizedRepoConfigDetected
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
729
1067
|
var GIT_EXCLUDE_PATTERNS = [`${AGENT_ENV_DIR}/`];
|
|
730
1068
|
async function ensureGitExclude(workspaceRoot, deps = defaultStateFsDeps) {
|
|
731
|
-
const excludePath =
|
|
1069
|
+
const excludePath = join3(workspaceRoot, ".git", "info", "exclude");
|
|
732
1070
|
try {
|
|
733
1071
|
const content = await deps.readFile(excludePath, "utf-8");
|
|
734
1072
|
const lines = content.split("\n");
|
|
@@ -751,15 +1089,16 @@ async function ensureGitExclude(workspaceRoot, deps = defaultStateFsDeps) {
|
|
|
751
1089
|
}
|
|
752
1090
|
|
|
753
1091
|
// src/lib/status-bar.ts
|
|
754
|
-
import { readFile as
|
|
755
|
-
import { join as
|
|
1092
|
+
import { readFile as readFile4, writeFile as writeFile4 } from "fs/promises";
|
|
1093
|
+
import { join as join4 } from "path";
|
|
756
1094
|
var STATUS_BAR_TEMPLATE_JSON = "statusBar.template.json";
|
|
757
1095
|
var STATUS_BAR_JSON = "statusBar.json";
|
|
1096
|
+
var TEMPLATE_NOT_FOUND = "TEMPLATE_NOT_FOUND";
|
|
758
1097
|
var PURPOSE_PLACEHOLDER = "{{PURPOSE}}";
|
|
759
1098
|
var NO_PURPOSE_TEXT = "(no purpose set)";
|
|
760
|
-
var defaultDeps = { readFile:
|
|
1099
|
+
var defaultDeps = { readFile: readFile4, writeFile: writeFile4 };
|
|
761
1100
|
async function regenerateStatusBar(workspaceRoot, agentEnvDir, purpose, deps = defaultDeps) {
|
|
762
|
-
const vscodeTemplatePath =
|
|
1101
|
+
const vscodeTemplatePath = join4(workspaceRoot, ".vscode", STATUS_BAR_TEMPLATE_JSON);
|
|
763
1102
|
let templateContent = null;
|
|
764
1103
|
try {
|
|
765
1104
|
templateContent = await deps.readFile(vscodeTemplatePath, "utf-8");
|
|
@@ -768,7 +1107,7 @@ async function regenerateStatusBar(workspaceRoot, agentEnvDir, purpose, deps = d
|
|
|
768
1107
|
throw err;
|
|
769
1108
|
}
|
|
770
1109
|
}
|
|
771
|
-
const agentEnvTemplatePath =
|
|
1110
|
+
const agentEnvTemplatePath = join4(agentEnvDir, STATUS_BAR_TEMPLATE_JSON);
|
|
772
1111
|
if (templateContent === null) {
|
|
773
1112
|
try {
|
|
774
1113
|
templateContent = await deps.readFile(agentEnvTemplatePath, "utf-8");
|
|
@@ -783,21 +1122,21 @@ async function regenerateStatusBar(workspaceRoot, agentEnvDir, purpose, deps = d
|
|
|
783
1122
|
new Error(
|
|
784
1123
|
`No status bar template found. Checked ${vscodeTemplatePath} and ${agentEnvTemplatePath}. Create a template manually or use \`agent-env init-template\` when available.`
|
|
785
1124
|
),
|
|
786
|
-
{ code:
|
|
1125
|
+
{ code: TEMPLATE_NOT_FOUND }
|
|
787
1126
|
);
|
|
788
1127
|
}
|
|
789
1128
|
const rawText = purpose ?? NO_PURPOSE_TEXT;
|
|
790
1129
|
const replacementText = JSON.stringify(rawText).slice(1, -1);
|
|
791
1130
|
const output = templateContent.replaceAll(PURPOSE_PLACEHOLDER, replacementText);
|
|
792
|
-
const outputPath =
|
|
1131
|
+
const outputPath = join4(agentEnvDir, STATUS_BAR_JSON);
|
|
793
1132
|
await deps.writeFile(outputPath, output, "utf-8");
|
|
794
1133
|
}
|
|
795
1134
|
|
|
796
1135
|
// src/lib/workspace.ts
|
|
797
1136
|
import { createHash } from "crypto";
|
|
798
|
-
import { mkdir as mkdir3, readdir as readdir2, readFile as
|
|
1137
|
+
import { mkdir as mkdir3, readdir as readdir2, readFile as readFile5, rm, stat as stat2 } from "fs/promises";
|
|
799
1138
|
import { homedir } from "os";
|
|
800
|
-
import { join as
|
|
1139
|
+
import { join as join5 } from "path";
|
|
801
1140
|
var defaultFsDeps2 = { mkdir: mkdir3, readdir: readdir2, stat: stat2, homedir };
|
|
802
1141
|
var VALID_NAME_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._-]*$/;
|
|
803
1142
|
function validateNameSegment(value, label) {
|
|
@@ -841,7 +1180,7 @@ function compressSlug(slug, maxLength = MAX_REPO_SLUG_LENGTH) {
|
|
|
841
1180
|
return `${prefix}_${hash}_${suffix}`;
|
|
842
1181
|
}
|
|
843
1182
|
function getWorkspacesBaseDir(deps = defaultFsDeps2) {
|
|
844
|
-
return
|
|
1183
|
+
return join5(deps.homedir(), AGENT_ENV_DIR, WORKSPACES_DIR);
|
|
845
1184
|
}
|
|
846
1185
|
function deriveWorkspaceName(repo, instance) {
|
|
847
1186
|
validateNameSegment(repo, "repo");
|
|
@@ -853,15 +1192,15 @@ function deriveContainerName(workspaceName) {
|
|
|
853
1192
|
}
|
|
854
1193
|
function getWorkspacePath(repo, instance, deps = defaultFsDeps2) {
|
|
855
1194
|
const name = deriveWorkspaceName(repo, instance);
|
|
856
|
-
const root =
|
|
857
|
-
const agentEnvDir =
|
|
858
|
-
const stateFile =
|
|
1195
|
+
const root = join5(getWorkspacesBaseDir(deps), name);
|
|
1196
|
+
const agentEnvDir = join5(root, AGENT_ENV_DIR);
|
|
1197
|
+
const stateFile = join5(agentEnvDir, STATE_FILE);
|
|
859
1198
|
return { root, name, agentEnvDir, stateFile };
|
|
860
1199
|
}
|
|
861
1200
|
function getWorkspacePathByName(workspaceName, deps = defaultFsDeps2) {
|
|
862
|
-
const root =
|
|
863
|
-
const agentEnvDir =
|
|
864
|
-
const stateFile =
|
|
1201
|
+
const root = join5(getWorkspacesBaseDir(deps), workspaceName);
|
|
1202
|
+
const agentEnvDir = join5(root, AGENT_ENV_DIR);
|
|
1203
|
+
const stateFile = join5(agentEnvDir, STATE_FILE);
|
|
865
1204
|
return { root, name: workspaceName, agentEnvDir, stateFile };
|
|
866
1205
|
}
|
|
867
1206
|
async function workspaceExists(repo, instance, deps = defaultFsDeps2) {
|
|
@@ -938,7 +1277,7 @@ async function resolveRepo(opts, executor) {
|
|
|
938
1277
|
}
|
|
939
1278
|
var defaultResolveInstanceDeps = {
|
|
940
1279
|
fsDeps: defaultFsDeps2,
|
|
941
|
-
readFile:
|
|
1280
|
+
readFile: readFile5
|
|
942
1281
|
};
|
|
943
1282
|
async function resolveInstance(instanceName, repoSlug, deps = defaultResolveInstanceDeps) {
|
|
944
1283
|
const workspaces = await scanWorkspaces(deps.fsDeps);
|
|
@@ -1000,10 +1339,21 @@ var MAX_PURPOSE_LENGTH = 200;
|
|
|
1000
1339
|
function createPurposeDefaultDeps() {
|
|
1001
1340
|
return {
|
|
1002
1341
|
workspaceFsDeps: { mkdir: mkdir4, readdir: readdir3, stat: stat3, homedir: homedir2 },
|
|
1003
|
-
stateFsDeps: { readFile:
|
|
1004
|
-
statusBarDeps: { readFile:
|
|
1342
|
+
stateFsDeps: { readFile: readFile6, writeFile: writeFile5, rename: rename3, mkdir: mkdir4, appendFile: appendFile2 },
|
|
1343
|
+
statusBarDeps: { readFile: readFile6, writeFile: writeFile5 }
|
|
1005
1344
|
};
|
|
1006
1345
|
}
|
|
1346
|
+
async function regenerateStatusBarSafe(workspaceRoot, agentEnvDir, purpose, statusBarDeps) {
|
|
1347
|
+
try {
|
|
1348
|
+
await regenerateStatusBar(workspaceRoot, agentEnvDir, purpose, statusBarDeps);
|
|
1349
|
+
} catch (err) {
|
|
1350
|
+
if (err.code === TEMPLATE_NOT_FOUND) {
|
|
1351
|
+
return `Status bar template not found \u2014 VS Code status bar won't update. Create one at .vscode/statusBar.template.json or ${agentEnvDir}/statusBar.template.json.`;
|
|
1352
|
+
}
|
|
1353
|
+
throw err;
|
|
1354
|
+
}
|
|
1355
|
+
return void 0;
|
|
1356
|
+
}
|
|
1007
1357
|
function validatePurposeLength(value) {
|
|
1008
1358
|
if (value.length > MAX_PURPOSE_LENGTH) {
|
|
1009
1359
|
return {
|
|
@@ -1047,28 +1397,19 @@ async function setPurpose(instanceName, value, deps, repoSlug) {
|
|
|
1047
1397
|
purpose: newPurpose
|
|
1048
1398
|
};
|
|
1049
1399
|
await writeStateAtomic(wsPath, updatedState, deps.stateFsDeps);
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
code: "TEMPLATE_NOT_FOUND",
|
|
1058
|
-
message: err.message,
|
|
1059
|
-
suggestion: "Create a status bar template at .vscode/statusBar.template.json or .agent-env/statusBar.template.json."
|
|
1060
|
-
}
|
|
1061
|
-
};
|
|
1062
|
-
}
|
|
1063
|
-
throw err;
|
|
1064
|
-
}
|
|
1065
|
-
return { ok: true, cleared };
|
|
1400
|
+
const warning = await regenerateStatusBarSafe(
|
|
1401
|
+
wsPath.root,
|
|
1402
|
+
wsPath.agentEnvDir,
|
|
1403
|
+
newPurpose,
|
|
1404
|
+
deps.statusBarDeps
|
|
1405
|
+
);
|
|
1406
|
+
return { ok: true, cleared, warning };
|
|
1066
1407
|
}
|
|
1067
1408
|
function createContainerPurposeDefaultDeps() {
|
|
1068
1409
|
return {
|
|
1069
|
-
stateFsDeps: { readFile:
|
|
1410
|
+
stateFsDeps: { readFile: readFile6, writeFile: writeFile5, rename: rename3, mkdir: mkdir4, appendFile: appendFile2 },
|
|
1070
1411
|
containerEnvDeps: { getEnv: (key) => process.env[key] },
|
|
1071
|
-
statusBarDeps: { readFile:
|
|
1412
|
+
statusBarDeps: { readFile: readFile6, writeFile: writeFile5 },
|
|
1072
1413
|
statePath: CONTAINER_STATE_PATH,
|
|
1073
1414
|
agentEnvDir: CONTAINER_AGENT_ENV_DIR,
|
|
1074
1415
|
workspaceRoot: process.cwd()
|
|
@@ -1087,7 +1428,7 @@ async function readContainerState(deps) {
|
|
|
1087
1428
|
}
|
|
1088
1429
|
};
|
|
1089
1430
|
}
|
|
1090
|
-
return { ok: true, state: parsed };
|
|
1431
|
+
return { ok: true, state: migrateConfigSource(parsed) };
|
|
1091
1432
|
} catch (err) {
|
|
1092
1433
|
if (err.code === "ENOENT") {
|
|
1093
1434
|
return {
|
|
@@ -1134,22 +1475,13 @@ async function setContainerPurpose(value, deps) {
|
|
|
1134
1475
|
stateFile: deps.statePath
|
|
1135
1476
|
};
|
|
1136
1477
|
await writeStateAtomic(wsPath, updatedState, deps.stateFsDeps);
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
code: "TEMPLATE_NOT_FOUND",
|
|
1145
|
-
message: err.message,
|
|
1146
|
-
suggestion: "Create a status bar template at .vscode/statusBar.template.json or .agent-env/statusBar.template.json."
|
|
1147
|
-
}
|
|
1148
|
-
};
|
|
1149
|
-
}
|
|
1150
|
-
throw err;
|
|
1151
|
-
}
|
|
1152
|
-
return { ok: true, cleared };
|
|
1478
|
+
const warning = await regenerateStatusBarSafe(
|
|
1479
|
+
deps.workspaceRoot,
|
|
1480
|
+
deps.agentEnvDir,
|
|
1481
|
+
newPurpose,
|
|
1482
|
+
deps.statusBarDeps
|
|
1483
|
+
);
|
|
1484
|
+
return { ok: true, cleared, warning };
|
|
1153
1485
|
}
|
|
1154
1486
|
|
|
1155
1487
|
// src/lib/create-instance.ts
|
|
@@ -1245,8 +1577,9 @@ function createDefaultDeps() {
|
|
|
1245
1577
|
executor,
|
|
1246
1578
|
container: createContainerLifecycle(executor),
|
|
1247
1579
|
workspaceFsDeps: { mkdir: mkdir5, readdir: readdir4, stat: stat4, homedir: homedir3 },
|
|
1248
|
-
stateFsDeps: { readFile:
|
|
1249
|
-
devcontainerFsDeps: { cp: cp2, mkdir: mkdir5, readFile:
|
|
1580
|
+
stateFsDeps: { readFile: readFile7, writeFile: writeFile6, rename: rename4, mkdir: mkdir5, appendFile: appendFile3 },
|
|
1581
|
+
devcontainerFsDeps: { cp: cp2, mkdir: mkdir5, readFile: readFile7, stat: stat4, writeFile: writeFile6 },
|
|
1582
|
+
mergeDeps: { readFile: readFile7, writeFile: writeFile6, rename: rename4, access: access3 },
|
|
1250
1583
|
rm: rm2,
|
|
1251
1584
|
logger: {
|
|
1252
1585
|
warn: (msg) => console.warn(msg),
|
|
@@ -1254,68 +1587,34 @@ function createDefaultDeps() {
|
|
|
1254
1587
|
}
|
|
1255
1588
|
};
|
|
1256
1589
|
}
|
|
1257
|
-
async function
|
|
1258
|
-
if (!hasConfig) {
|
|
1259
|
-
return "baseline";
|
|
1260
|
-
}
|
|
1261
|
-
if (baselineChoice === "force-baseline") {
|
|
1262
|
-
return "baseline";
|
|
1263
|
-
}
|
|
1264
|
-
if (baselineChoice === "force-repo-config") {
|
|
1265
|
-
return "repo";
|
|
1266
|
-
}
|
|
1267
|
-
if (askUser) {
|
|
1268
|
-
return askUser();
|
|
1269
|
-
}
|
|
1270
|
-
return "repo";
|
|
1271
|
-
}
|
|
1272
|
-
function deriveBaselineChoice(baseline) {
|
|
1273
|
-
if (baseline === true) return "force-baseline";
|
|
1274
|
-
if (baseline === false) return "force-repo-config";
|
|
1275
|
-
return "ask-user";
|
|
1276
|
-
}
|
|
1277
|
-
async function setupDevcontainerConfig(wsPath, containerName, repoName, purposeText, deps, baselineChoice = "ask-user", askUser) {
|
|
1278
|
-
const hasConfig = await hasDevcontainerConfig(wsPath.root, deps.devcontainerFsDeps);
|
|
1279
|
-
const choice = await resolveConfigChoice(hasConfig, baselineChoice, askUser);
|
|
1280
|
-
if (choice === "repo") {
|
|
1281
|
-
return { ok: true, configSource: "repo" };
|
|
1282
|
-
}
|
|
1283
|
-
try {
|
|
1284
|
-
await copyBaselineConfig(wsPath.root, deps.devcontainerFsDeps);
|
|
1285
|
-
await copyStatusBarTemplate(wsPath.root, deps.devcontainerFsDeps);
|
|
1286
|
-
} catch (err) {
|
|
1287
|
-
await safeRollback(wsPath.root, deps.rm, deps.logger);
|
|
1288
|
-
return {
|
|
1289
|
-
ok: false,
|
|
1290
|
-
error: {
|
|
1291
|
-
code: "CONTAINER_ERROR",
|
|
1292
|
-
message: `Failed to copy baseline devcontainer config: ${err instanceof Error ? err.message : String(err)}`
|
|
1293
|
-
}
|
|
1294
|
-
};
|
|
1295
|
-
}
|
|
1590
|
+
async function setupMergedConfig(wsPath, containerName, repoName, purposeText, deps) {
|
|
1296
1591
|
try {
|
|
1297
|
-
await
|
|
1298
|
-
|
|
1592
|
+
const defaults = await loadManagedDefaults(deps.mergeDeps);
|
|
1593
|
+
const managed = buildManagedConfig(defaults, {
|
|
1594
|
+
instanceName: wsPath.name,
|
|
1299
1595
|
containerName,
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
deps.
|
|
1306
|
-
|
|
1307
|
-
);
|
|
1596
|
+
repoSlug: repoName,
|
|
1597
|
+
purpose: purposeText
|
|
1598
|
+
});
|
|
1599
|
+
const repoConfig = await readRepoConfig(wsPath.root, deps.mergeDeps, deps.logger);
|
|
1600
|
+
if (repoConfig) {
|
|
1601
|
+
validateRepoConfig(repoConfig, deps.logger);
|
|
1602
|
+
}
|
|
1603
|
+
const merged = mergeDevcontainerConfigs(managed, repoConfig);
|
|
1604
|
+
await copyManagedAssets(wsPath.root, deps.devcontainerFsDeps);
|
|
1605
|
+
const configPath = join6(wsPath.root, AGENT_ENV_DIR, "devcontainer.json");
|
|
1606
|
+
await writeGeneratedConfig(configPath, merged, deps.mergeDeps);
|
|
1607
|
+
return { ok: true, repoConfigDetected: repoConfig !== void 0 };
|
|
1308
1608
|
} catch (err) {
|
|
1309
1609
|
await safeRollback(wsPath.root, deps.rm, deps.logger);
|
|
1310
1610
|
return {
|
|
1311
1611
|
ok: false,
|
|
1312
1612
|
error: {
|
|
1313
1613
|
code: "CONTAINER_ERROR",
|
|
1314
|
-
message: `Failed to
|
|
1614
|
+
message: `Failed to set up devcontainer config: ${err instanceof Error ? err.message : String(err)}`
|
|
1315
1615
|
}
|
|
1316
1616
|
};
|
|
1317
1617
|
}
|
|
1318
|
-
return { ok: true, configSource: "baseline" };
|
|
1319
1618
|
}
|
|
1320
1619
|
async function cloneAndPrepareWorkspace(repoUrl, wsPath, deps) {
|
|
1321
1620
|
try {
|
|
@@ -1405,20 +1704,11 @@ async function createInstance(instanceName, repoUrl, deps, options) {
|
|
|
1405
1704
|
}
|
|
1406
1705
|
const prepResult = await cloneAndPrepareWorkspace(repoUrl, wsPath, deps);
|
|
1407
1706
|
if (!prepResult.ok) return prepResult;
|
|
1408
|
-
const
|
|
1409
|
-
const configResult = await setupDevcontainerConfig(
|
|
1410
|
-
wsPath,
|
|
1411
|
-
containerName,
|
|
1412
|
-
repoName,
|
|
1413
|
-
purposeText,
|
|
1414
|
-
deps,
|
|
1415
|
-
baselineChoice,
|
|
1416
|
-
options?.askUser
|
|
1417
|
-
);
|
|
1707
|
+
const configResult = await setupMergedConfig(wsPath, containerName, repoName, purposeText, deps);
|
|
1418
1708
|
if (!configResult.ok) {
|
|
1419
1709
|
return configResult;
|
|
1420
1710
|
}
|
|
1421
|
-
const {
|
|
1711
|
+
const { repoConfigDetected } = configResult;
|
|
1422
1712
|
const existingContainer = await deps.container.findContainerByWorkspaceLabel(wsPath.root);
|
|
1423
1713
|
if (existingContainer) {
|
|
1424
1714
|
await safeRollback(wsPath.root, deps.rm, deps.logger);
|
|
@@ -1431,13 +1721,13 @@ async function createInstance(instanceName, repoUrl, deps, options) {
|
|
|
1431
1721
|
}
|
|
1432
1722
|
};
|
|
1433
1723
|
}
|
|
1434
|
-
const
|
|
1724
|
+
const configPath = join6(wsPath.root, AGENT_ENV_DIR, "devcontainer.json");
|
|
1435
1725
|
const containerResult = await deps.container.devcontainerUp(wsPath.root, containerName, {
|
|
1436
1726
|
remoteEnv: {
|
|
1437
1727
|
AGENT_INSTANCE: wsPath.name,
|
|
1438
1728
|
AGENT_ENV_PURPOSE: purposeText
|
|
1439
1729
|
},
|
|
1440
|
-
configPath
|
|
1730
|
+
configPath
|
|
1441
1731
|
});
|
|
1442
1732
|
if (!containerResult.ok) {
|
|
1443
1733
|
return rollbackContainerFailure(containerName, wsPath, containerResult, deps);
|
|
@@ -1449,7 +1739,7 @@ async function createInstance(instanceName, repoUrl, deps, options) {
|
|
|
1449
1739
|
);
|
|
1450
1740
|
const state = createInitialState(instanceName, repoName, repoUrl, {
|
|
1451
1741
|
containerName: actualContainerName,
|
|
1452
|
-
|
|
1742
|
+
repoConfigDetected,
|
|
1453
1743
|
purpose: purposeState
|
|
1454
1744
|
});
|
|
1455
1745
|
await writeStateAtomic(wsPath, state, deps.stateFsDeps);
|
|
@@ -1527,7 +1817,7 @@ function createAttachDefaultDeps() {
|
|
|
1527
1817
|
executor,
|
|
1528
1818
|
container: createContainerLifecycle(executor),
|
|
1529
1819
|
workspaceFsDeps: { mkdir: mkdir6, readdir: readdir5, stat: stat5, homedir: homedir4 },
|
|
1530
|
-
stateFsDeps: { readFile:
|
|
1820
|
+
stateFsDeps: { readFile: readFile8, writeFile: writeFile7, rename: rename5, mkdir: mkdir6, appendFile: appendFile4 }
|
|
1531
1821
|
};
|
|
1532
1822
|
}
|
|
1533
1823
|
async function attachInstance(instanceName, deps, onContainerStarting, onAttaching, repoSlug) {
|
|
@@ -1564,11 +1854,10 @@ async function attachInstance(instanceName, deps, onContainerStarting, onAttachi
|
|
|
1564
1854
|
}
|
|
1565
1855
|
if (statusResult.status === "stopped" || statusResult.status === "not-found") {
|
|
1566
1856
|
onContainerStarting?.();
|
|
1567
|
-
const
|
|
1568
|
-
const baselineConfigPath = configSource === "baseline" ? join6(wsPath.root, AGENT_ENV_DIR, "devcontainer.json") : void 0;
|
|
1857
|
+
const configPath = join7(wsPath.root, AGENT_ENV_DIR, "devcontainer.json");
|
|
1569
1858
|
const startResult = await deps.container.devcontainerUp(wsPath.root, containerName, {
|
|
1570
1859
|
remoteEnv: { AGENT_INSTANCE: wsPath.name },
|
|
1571
|
-
configPath
|
|
1860
|
+
configPath
|
|
1572
1861
|
});
|
|
1573
1862
|
if (!startResult.ok) {
|
|
1574
1863
|
return {
|
|
@@ -1814,7 +2103,6 @@ ${getInstallInstructions()}`).argument("<shell>", "Shell type (bash or zsh)").ac
|
|
|
1814
2103
|
|
|
1815
2104
|
// src/commands/create.ts
|
|
1816
2105
|
import { Command as Command3 } from "commander";
|
|
1817
|
-
import { createInterface } from "readline";
|
|
1818
2106
|
|
|
1819
2107
|
// src/lib/list-repos.ts
|
|
1820
2108
|
async function listRepos(deps) {
|
|
@@ -1894,47 +2182,26 @@ async function resolveRepoArg(repoArg, deps) {
|
|
|
1894
2182
|
}
|
|
1895
2183
|
|
|
1896
2184
|
// src/commands/create.ts
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
if (!process.stdin.isTTY) {
|
|
1900
|
-
return "repo";
|
|
1901
|
-
}
|
|
1902
|
-
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
1903
|
-
return new Promise((resolve2) => {
|
|
1904
|
-
console.log();
|
|
1905
|
-
console.log("This repo has a .devcontainer/ config.");
|
|
1906
|
-
console.log(" [1] Use repo config (default)");
|
|
1907
|
-
console.log(" [2] Use agent-env baseline");
|
|
1908
|
-
rl.question("Choose [1/2]: ", (answer) => {
|
|
1909
|
-
rl.close();
|
|
1910
|
-
resolve2(answer.trim() === "2" ? "baseline" : "repo");
|
|
1911
|
-
});
|
|
1912
|
-
});
|
|
1913
|
-
};
|
|
1914
|
-
}
|
|
1915
|
-
var createCommand = new Command3("create").description("Create a new isolated development environment").argument("<name>", "Name for the new instance").option("--repo <url|slug>", 'Git repo URL, registered slug, or "." for current directory').option("--purpose <text>", "Set the instance purpose/label at creation time").option("--attach", "Attach immediately after creation").option("--baseline", "Force agent-env baseline config (ignore repo .devcontainer/)").option("--no-baseline", "Force repo config (skip baseline prompt)").action(async (name, options) => {
|
|
1916
|
-
if (!options.repo) {
|
|
2185
|
+
var createCommand = new Command3("create").description("Create a new isolated development environment").argument("<name>", "Name for the new instance").option("--repo <url|slug>", 'Git repo URL, registered slug, or "." for current directory').option("--purpose <text>", "Set the instance purpose/label at creation time").option("--attach", "Attach immediately after creation").option("--baseline", "[REMOVED] Config merge replaces this flag").option("--no-baseline", "[REMOVED] Config merge replaces this flag").action(async (name, options) => {
|
|
2186
|
+
if (options.baseline !== void 0) {
|
|
1917
2187
|
console.error(
|
|
1918
2188
|
formatError(
|
|
1919
2189
|
createError(
|
|
1920
|
-
"
|
|
1921
|
-
"The --
|
|
1922
|
-
"
|
|
2190
|
+
"REMOVED_OPTION",
|
|
2191
|
+
"The --baseline / --no-baseline flags have been removed.",
|
|
2192
|
+
"Agent-env now always merges managed config with repo config. No flag needed."
|
|
1923
2193
|
)
|
|
1924
2194
|
)
|
|
1925
2195
|
);
|
|
1926
2196
|
process.exit(1);
|
|
1927
2197
|
}
|
|
1928
|
-
|
|
1929
|
-
const hasBaselineFlag = argv.includes("--baseline");
|
|
1930
|
-
const hasNoBaselineFlag = argv.includes("--no-baseline");
|
|
1931
|
-
if (hasBaselineFlag && hasNoBaselineFlag) {
|
|
2198
|
+
if (!options.repo) {
|
|
1932
2199
|
console.error(
|
|
1933
2200
|
formatError(
|
|
1934
2201
|
createError(
|
|
1935
|
-
"
|
|
1936
|
-
"
|
|
1937
|
-
"
|
|
2202
|
+
"MISSING_OPTION",
|
|
2203
|
+
"The --repo flag is required.",
|
|
2204
|
+
"Usage: agent-env create <name> --repo <url|slug>"
|
|
1938
2205
|
)
|
|
1939
2206
|
)
|
|
1940
2207
|
);
|
|
@@ -1959,11 +2226,8 @@ var createCommand = new Command3("create").description("Create a new isolated de
|
|
|
1959
2226
|
}
|
|
1960
2227
|
const repoUrl = resolved.url;
|
|
1961
2228
|
console.log(`Creating instance '${name}' from ${repoUrl}...`);
|
|
1962
|
-
const baselineOption = hasBaselineFlag ? true : hasNoBaselineFlag ? false : void 0;
|
|
1963
2229
|
const result = await createInstance(name, repoUrl, deps, {
|
|
1964
|
-
purpose: options.purpose
|
|
1965
|
-
baseline: baselineOption,
|
|
1966
|
-
askUser: createBaselinePrompt()
|
|
2230
|
+
purpose: options.purpose
|
|
1967
2231
|
});
|
|
1968
2232
|
if (!result.ok) {
|
|
1969
2233
|
const { code, message, suggestion } = result.error;
|
|
@@ -2459,101 +2723,83 @@ function handleSetResult(result) {
|
|
|
2459
2723
|
} else {
|
|
2460
2724
|
console.log("Purpose updated");
|
|
2461
2725
|
}
|
|
2726
|
+
if (result.warning) {
|
|
2727
|
+
console.warn(`\x1B[33m\u26A0\x1B[0m ${result.warning}`);
|
|
2728
|
+
}
|
|
2462
2729
|
}
|
|
2463
2730
|
|
|
2464
2731
|
// src/commands/rebuild.ts
|
|
2465
2732
|
import chalk from "chalk";
|
|
2466
2733
|
import { Command as Command6 } from "commander";
|
|
2467
|
-
import { createInterface
|
|
2734
|
+
import { createInterface } from "readline";
|
|
2468
2735
|
|
|
2469
2736
|
// src/lib/rebuild-instance.ts
|
|
2470
2737
|
import {
|
|
2738
|
+
access as access4,
|
|
2471
2739
|
cp as cp3,
|
|
2472
2740
|
mkdir as mkdir7,
|
|
2473
2741
|
readdir as readdir6,
|
|
2474
|
-
readFile as
|
|
2475
|
-
rename as
|
|
2742
|
+
readFile as readFile9,
|
|
2743
|
+
rename as rename6,
|
|
2476
2744
|
rm as rm3,
|
|
2477
2745
|
stat as stat6,
|
|
2478
|
-
writeFile as
|
|
2746
|
+
writeFile as writeFile8,
|
|
2479
2747
|
appendFile as appendFile5
|
|
2480
2748
|
} from "fs/promises";
|
|
2481
2749
|
import { homedir as homedir5 } from "os";
|
|
2482
|
-
import { join as
|
|
2750
|
+
import { join as join8 } from "path";
|
|
2483
2751
|
function createRebuildDefaultDeps() {
|
|
2484
2752
|
const executor = createExecutor();
|
|
2485
2753
|
return {
|
|
2486
2754
|
executor,
|
|
2487
2755
|
container: createContainerLifecycle(executor),
|
|
2488
2756
|
workspaceFsDeps: { mkdir: mkdir7, readdir: readdir6, stat: stat6, homedir: homedir5 },
|
|
2489
|
-
stateFsDeps: { readFile:
|
|
2490
|
-
devcontainerFsDeps: { cp: cp3, mkdir: mkdir7, readdir: readdir6, readFile:
|
|
2757
|
+
stateFsDeps: { readFile: readFile9, writeFile: writeFile8, rename: rename6, mkdir: mkdir7, appendFile: appendFile5 },
|
|
2758
|
+
devcontainerFsDeps: { cp: cp3, mkdir: mkdir7, readdir: readdir6, readFile: readFile9, stat: stat6, writeFile: writeFile8 },
|
|
2759
|
+
mergeDeps: { readFile: readFile9, writeFile: writeFile8, rename: rename6, access: access4 },
|
|
2491
2760
|
rm: rm3,
|
|
2492
|
-
rename:
|
|
2761
|
+
rename: rename6,
|
|
2493
2762
|
logger: { warn: (msg) => console.warn(msg), info: (msg) => console.info(msg) }
|
|
2494
2763
|
};
|
|
2495
2764
|
}
|
|
2496
|
-
async function
|
|
2497
|
-
|
|
2498
|
-
const
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
} catch (err) {
|
|
2508
|
-
if (err.code !== "ENOENT") throw err;
|
|
2509
|
-
await copyStatusBarTemplate(wsRoot, deps.devcontainerFsDeps);
|
|
2510
|
-
}
|
|
2511
|
-
await applyBaselinePatches(
|
|
2512
|
-
wsRoot,
|
|
2513
|
-
containerName,
|
|
2514
|
-
envVars,
|
|
2515
|
-
deps.devcontainerFsDeps,
|
|
2516
|
-
AGENT_ENV_DIR
|
|
2517
|
-
);
|
|
2518
|
-
} catch (err) {
|
|
2765
|
+
async function refreshMergedConfig(wsRoot, containerName, wsName, repoSlug, purpose, repoConfigExpected, deps) {
|
|
2766
|
+
try {
|
|
2767
|
+
const defaults = await loadManagedDefaults(deps.mergeDeps);
|
|
2768
|
+
const managed = buildManagedConfig(defaults, {
|
|
2769
|
+
instanceName: wsName,
|
|
2770
|
+
containerName,
|
|
2771
|
+
repoSlug,
|
|
2772
|
+
purpose
|
|
2773
|
+
});
|
|
2774
|
+
const repoConfig = await readRepoConfig(wsRoot, deps.mergeDeps, deps.logger);
|
|
2775
|
+
if (repoConfigExpected && !repoConfig) {
|
|
2519
2776
|
return {
|
|
2520
2777
|
ok: false,
|
|
2521
2778
|
error: {
|
|
2522
|
-
code: "
|
|
2523
|
-
message: `
|
|
2524
|
-
suggestion: "
|
|
2779
|
+
code: "REPO_CONFIG_MISSING",
|
|
2780
|
+
message: `Repo devcontainer config was present at creation but is now missing from ${wsRoot}`,
|
|
2781
|
+
suggestion: "Restore the repo config, or recreate the instance with `agent-env create` to generate a managed-only config."
|
|
2525
2782
|
}
|
|
2526
2783
|
};
|
|
2527
2784
|
}
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
if (!configExists) {
|
|
2531
|
-
return {
|
|
2532
|
-
ok: false,
|
|
2533
|
-
error: {
|
|
2534
|
-
code: "CONFIG_MISSING",
|
|
2535
|
-
message: "Repo-provided devcontainer config is missing.",
|
|
2536
|
-
suggestion: "Re-clone the repository or provide a devcontainer config manually."
|
|
2537
|
-
}
|
|
2538
|
-
};
|
|
2785
|
+
if (repoConfig) {
|
|
2786
|
+
validateRepoConfig(repoConfig, deps.logger);
|
|
2539
2787
|
}
|
|
2540
|
-
const
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
};
|
|
2788
|
+
const merged = mergeDevcontainerConfigs(managed, repoConfig);
|
|
2789
|
+
await copyManagedAssets(wsRoot, deps.devcontainerFsDeps);
|
|
2790
|
+
const configPath = join8(wsRoot, AGENT_ENV_DIR, "devcontainer.json");
|
|
2791
|
+
await writeGeneratedConfig(configPath, merged, deps.mergeDeps);
|
|
2792
|
+
return { ok: true, repoConfigDetected: repoConfig !== void 0 };
|
|
2793
|
+
} catch (err) {
|
|
2794
|
+
return {
|
|
2795
|
+
ok: false,
|
|
2796
|
+
error: {
|
|
2797
|
+
code: "CONFIG_REFRESH_FAILED",
|
|
2798
|
+
message: `Failed to refresh config: ${err instanceof Error ? err.message : String(err)}`,
|
|
2799
|
+
suggestion: "The existing container is still intact. Check that the baseline config files exist in the package."
|
|
2553
2800
|
}
|
|
2554
|
-
}
|
|
2801
|
+
};
|
|
2555
2802
|
}
|
|
2556
|
-
return { ok: true };
|
|
2557
2803
|
}
|
|
2558
2804
|
async function lookupWorkspace(instanceName, deps, repoSlug) {
|
|
2559
2805
|
const lookup = await resolveInstance(instanceName, repoSlug, {
|
|
@@ -2640,12 +2886,12 @@ async function teardownContainer(containerName, container, statusResult) {
|
|
|
2640
2886
|
}
|
|
2641
2887
|
return { ok: true };
|
|
2642
2888
|
}
|
|
2643
|
-
async function startAndDiscoverContainer(wsPath, containerName,
|
|
2644
|
-
const
|
|
2889
|
+
async function startAndDiscoverContainer(wsPath, containerName, purpose, buildNoCache, container) {
|
|
2890
|
+
const configPath = join8(wsPath.root, AGENT_ENV_DIR, "devcontainer.json");
|
|
2645
2891
|
const containerResult = await container.devcontainerUp(wsPath.root, containerName, {
|
|
2646
2892
|
buildNoCache,
|
|
2647
2893
|
remoteEnv: { AGENT_INSTANCE: wsPath.name, AGENT_ENV_PURPOSE: purpose },
|
|
2648
|
-
configPath
|
|
2894
|
+
configPath
|
|
2649
2895
|
});
|
|
2650
2896
|
if (!containerResult.ok) {
|
|
2651
2897
|
return { ok: false, error: containerResult.error };
|
|
@@ -2660,12 +2906,7 @@ async function startAndDiscoverContainer(wsPath, containerName, configSource, pu
|
|
|
2660
2906
|
return { ok: true, containerName: actualName };
|
|
2661
2907
|
}
|
|
2662
2908
|
async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
2663
|
-
const {
|
|
2664
|
-
force = false,
|
|
2665
|
-
configSource: overrideConfigSource,
|
|
2666
|
-
pull = true,
|
|
2667
|
-
noCache = true
|
|
2668
|
-
} = options ?? {};
|
|
2909
|
+
const { force = false, pull = true, noCache = true } = options ?? {};
|
|
2669
2910
|
const wsLookup = await lookupWorkspace(instanceName, deps, repoSlug);
|
|
2670
2911
|
if (!wsLookup.ok) {
|
|
2671
2912
|
return { ok: false, error: wsLookup.error };
|
|
@@ -2683,23 +2924,15 @@ async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
|
2683
2924
|
}
|
|
2684
2925
|
};
|
|
2685
2926
|
}
|
|
2686
|
-
const
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
};
|
|
2696
|
-
}
|
|
2697
|
-
const envVars = {
|
|
2698
|
-
AGENT_ENV_INSTANCE: wsPath.name,
|
|
2699
|
-
AGENT_ENV_REPO: state.repoSlug ?? "",
|
|
2700
|
-
AGENT_ENV_PURPOSE: state.purpose ?? ""
|
|
2701
|
-
};
|
|
2702
|
-
const configResult = await refreshConfig(wsPath.root, containerName, configSource, envVars, deps);
|
|
2927
|
+
const configResult = await refreshMergedConfig(
|
|
2928
|
+
wsPath.root,
|
|
2929
|
+
containerName,
|
|
2930
|
+
wsPath.name,
|
|
2931
|
+
state.repoSlug ?? "",
|
|
2932
|
+
state.purpose ?? "",
|
|
2933
|
+
state.repoConfigDetected,
|
|
2934
|
+
deps
|
|
2935
|
+
);
|
|
2703
2936
|
if (!configResult.ok) {
|
|
2704
2937
|
return { ok: false, error: configResult.error };
|
|
2705
2938
|
}
|
|
@@ -2728,7 +2961,6 @@ async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
|
2728
2961
|
const startResult = await startAndDiscoverContainer(
|
|
2729
2962
|
wsPath,
|
|
2730
2963
|
containerName,
|
|
2731
|
-
configSource,
|
|
2732
2964
|
state.purpose ?? "",
|
|
2733
2965
|
noCache && hasDockerfile,
|
|
2734
2966
|
deps.container
|
|
@@ -2739,7 +2971,8 @@ async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
|
2739
2971
|
const updatedState = {
|
|
2740
2972
|
...state,
|
|
2741
2973
|
containerName: startResult.containerName,
|
|
2742
|
-
lastRebuilt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2974
|
+
lastRebuilt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2975
|
+
repoConfigDetected: configResult.repoConfigDetected
|
|
2743
2976
|
};
|
|
2744
2977
|
await writeStateAtomic(wsPath, updatedState, deps.stateFsDeps);
|
|
2745
2978
|
return {
|
|
@@ -2752,7 +2985,7 @@ async function rebuildInstance(instanceName, deps, options, repoSlug) {
|
|
|
2752
2985
|
// src/commands/rebuild.ts
|
|
2753
2986
|
function promptForConfirmation(instanceName) {
|
|
2754
2987
|
return new Promise((resolve2) => {
|
|
2755
|
-
const rl =
|
|
2988
|
+
const rl = createInterface({
|
|
2756
2989
|
input: process.stdin,
|
|
2757
2990
|
output: process.stderr
|
|
2758
2991
|
});
|
|
@@ -2818,21 +3051,21 @@ var rebuildCommand = new Command6("rebuild").description("Rebuild an instance by
|
|
|
2818
3051
|
// src/commands/remove.ts
|
|
2819
3052
|
import chalk3 from "chalk";
|
|
2820
3053
|
import { Command as Command7 } from "commander";
|
|
2821
|
-
import { createInterface as
|
|
3054
|
+
import { createInterface as createInterface2 } from "readline";
|
|
2822
3055
|
|
|
2823
3056
|
// src/lib/audit-log.ts
|
|
2824
3057
|
import { appendFile as appendFile6, mkdir as mkdir8 } from "fs/promises";
|
|
2825
3058
|
import { homedir as homedir6 } from "os";
|
|
2826
|
-
import { join as
|
|
3059
|
+
import { join as join9 } from "path";
|
|
2827
3060
|
function createAuditLogDefaultDeps() {
|
|
2828
3061
|
return { appendFile: appendFile6, mkdir: mkdir8, homedir: homedir6 };
|
|
2829
3062
|
}
|
|
2830
3063
|
function getAuditLogPath(deps) {
|
|
2831
|
-
return
|
|
3064
|
+
return join9(deps.homedir(), AGENT_ENV_DIR, "audit.log");
|
|
2832
3065
|
}
|
|
2833
3066
|
async function writeAuditLogEntry(entry, deps = createAuditLogDefaultDeps()) {
|
|
2834
3067
|
const logPath = getAuditLogPath(deps);
|
|
2835
|
-
await deps.mkdir(
|
|
3068
|
+
await deps.mkdir(join9(deps.homedir(), AGENT_ENV_DIR), { recursive: true });
|
|
2836
3069
|
const line = JSON.stringify(entry) + "\n";
|
|
2837
3070
|
await deps.appendFile(logPath, line, "utf-8");
|
|
2838
3071
|
}
|
|
@@ -2847,7 +3080,7 @@ function createAuditEntry(instanceName, gitState, confirmationMethod) {
|
|
|
2847
3080
|
}
|
|
2848
3081
|
|
|
2849
3082
|
// src/lib/remove-instance.ts
|
|
2850
|
-
import { mkdir as mkdir9, readdir as readdir7, readFile as
|
|
3083
|
+
import { mkdir as mkdir9, readdir as readdir7, readFile as readFile10, rm as rm4, stat as stat7 } from "fs/promises";
|
|
2851
3084
|
import { homedir as homedir7 } from "os";
|
|
2852
3085
|
function createRemoveDefaultDeps() {
|
|
2853
3086
|
const executor = createExecutor();
|
|
@@ -2856,7 +3089,7 @@ function createRemoveDefaultDeps() {
|
|
|
2856
3089
|
container: createContainerLifecycle(executor),
|
|
2857
3090
|
gitDetector: createGitStateDetector(executor),
|
|
2858
3091
|
workspaceFsDeps: { mkdir: mkdir9, readdir: readdir7, stat: stat7, homedir: homedir7 },
|
|
2859
|
-
stateFsDeps: { readFile:
|
|
3092
|
+
stateFsDeps: { readFile: readFile10 },
|
|
2860
3093
|
deleteFsDeps: { rm: rm4 }
|
|
2861
3094
|
};
|
|
2862
3095
|
}
|
|
@@ -3076,7 +3309,7 @@ function formatSafetyReport(instanceName, blockers, gitState) {
|
|
|
3076
3309
|
function promptForConfirmation2(instanceName) {
|
|
3077
3310
|
return new Promise((resolve2) => {
|
|
3078
3311
|
let resolved = false;
|
|
3079
|
-
const rl =
|
|
3312
|
+
const rl = createInterface2({
|
|
3080
3313
|
input: process.stdin,
|
|
3081
3314
|
output: process.stderr
|
|
3082
3315
|
});
|
|
@@ -3250,6 +3483,59 @@ var reposCommand = new Command8("repos").description("List tracked repositories"
|
|
|
3250
3483
|
}
|
|
3251
3484
|
});
|
|
3252
3485
|
|
|
3486
|
+
// src/commands/tmux-status.ts
|
|
3487
|
+
import { Command as Command9 } from "commander";
|
|
3488
|
+
|
|
3489
|
+
// src/lib/tmux-status.ts
|
|
3490
|
+
import { readFile as readFile11 } from "fs/promises";
|
|
3491
|
+
var MAX_PURPOSE_DISPLAY_LEN = 40;
|
|
3492
|
+
var defaultDeps2 = { readFile: readFile11 };
|
|
3493
|
+
function formatTmuxStatus(instance, purpose) {
|
|
3494
|
+
if (!instance) return "?";
|
|
3495
|
+
if (!purpose) return instance;
|
|
3496
|
+
if (purpose.length > MAX_PURPOSE_DISPLAY_LEN) {
|
|
3497
|
+
return `${instance} | ${purpose.slice(0, MAX_PURPOSE_DISPLAY_LEN)}\u2026`;
|
|
3498
|
+
}
|
|
3499
|
+
return `${instance} | ${purpose}`;
|
|
3500
|
+
}
|
|
3501
|
+
async function getTmuxStatus(statePath, deps = defaultDeps2) {
|
|
3502
|
+
let content;
|
|
3503
|
+
try {
|
|
3504
|
+
content = await deps.readFile(statePath, "utf-8");
|
|
3505
|
+
} catch {
|
|
3506
|
+
return "?";
|
|
3507
|
+
}
|
|
3508
|
+
let parsed;
|
|
3509
|
+
try {
|
|
3510
|
+
parsed = JSON.parse(content);
|
|
3511
|
+
} catch {
|
|
3512
|
+
return "?";
|
|
3513
|
+
}
|
|
3514
|
+
if (typeof parsed !== "object" || parsed === null) return "?";
|
|
3515
|
+
const obj = parsed;
|
|
3516
|
+
const instance = typeof obj.instance === "string" ? obj.instance : "";
|
|
3517
|
+
const purpose = typeof obj.purpose === "string" ? obj.purpose : null;
|
|
3518
|
+
return formatTmuxStatus(instance, purpose);
|
|
3519
|
+
}
|
|
3520
|
+
|
|
3521
|
+
// src/commands/tmux-status.ts
|
|
3522
|
+
var defaultDeps3 = {
|
|
3523
|
+
isInsideContainer,
|
|
3524
|
+
getTmuxStatus,
|
|
3525
|
+
statePath: CONTAINER_STATE_PATH
|
|
3526
|
+
};
|
|
3527
|
+
function createTmuxStatusCommand(deps = defaultDeps3) {
|
|
3528
|
+
return new Command9("tmux-status").description("Output formatted instance status for tmux status bar (container only)").action(async () => {
|
|
3529
|
+
if (!deps.isInsideContainer()) {
|
|
3530
|
+
console.log("?");
|
|
3531
|
+
return;
|
|
3532
|
+
}
|
|
3533
|
+
const display = await deps.getTmuxStatus(deps.statePath);
|
|
3534
|
+
console.log(display);
|
|
3535
|
+
});
|
|
3536
|
+
}
|
|
3537
|
+
var tmuxStatusCommand = createTmuxStatusCommand();
|
|
3538
|
+
|
|
3253
3539
|
// src/components/InteractiveMenu.tsx
|
|
3254
3540
|
import { Select } from "@inkjs/ui";
|
|
3255
3541
|
import { Box as Box2, Text as Text3 } from "ink";
|
|
@@ -3433,6 +3719,7 @@ program.addCommand(rebuildCommand);
|
|
|
3433
3719
|
program.addCommand(removeCommand);
|
|
3434
3720
|
program.addCommand(purposeCommand);
|
|
3435
3721
|
program.addCommand(reposCommand);
|
|
3722
|
+
program.addCommand(tmuxStatusCommand);
|
|
3436
3723
|
program.addCommand(completionCommand);
|
|
3437
3724
|
program.action(async () => {
|
|
3438
3725
|
if (!process.stdin.isTTY) {
|