@leitwerk-dev/worker 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE +13 -0
- package/dist/automatic-turn-executor.d.ts +28 -0
- package/dist/automatic-turn-executor.d.ts.map +1 -0
- package/dist/automatic-turn-executor.js +58 -0
- package/dist/automatic-turn-executor.js.map +1 -0
- package/dist/chunk-N5HLJDWA.js +8124 -0
- package/dist/chunk-N5HLJDWA.js.map +1 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +51 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/entry-runtime.d.ts +24 -0
- package/dist/entry-runtime.d.ts.map +1 -0
- package/dist/entry-runtime.js +160 -0
- package/dist/entry-runtime.js.map +1 -0
- package/dist/event-translator.d.ts +8 -0
- package/dist/event-translator.d.ts.map +1 -0
- package/dist/event-translator.js +12 -0
- package/dist/event-translator.js.map +1 -0
- package/dist/extension-entry-roots.d.ts +7 -0
- package/dist/extension-entry-roots.d.ts.map +1 -0
- package/dist/extension-entry-roots.js +41 -0
- package/dist/extension-entry-roots.js.map +1 -0
- package/dist/identified-pi-entry.d.ts +56 -0
- package/dist/identified-pi-entry.d.ts.map +1 -0
- package/dist/identified-pi-entry.js +125 -0
- package/dist/identified-pi-entry.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/input-consumer.d.ts +24 -0
- package/dist/input-consumer.d.ts.map +1 -0
- package/dist/input-consumer.js +34 -0
- package/dist/input-consumer.js.map +1 -0
- package/dist/ipc.d.ts +22 -0
- package/dist/ipc.d.ts.map +1 -0
- package/dist/ipc.js +192 -0
- package/dist/ipc.js.map +1 -0
- package/dist/logical-prompt-runner.d.ts +49 -0
- package/dist/logical-prompt-runner.d.ts.map +1 -0
- package/dist/logical-prompt-runner.js +225 -0
- package/dist/logical-prompt-runner.js.map +1 -0
- package/dist/managed-pi-agent-dir.d.ts +13 -0
- package/dist/managed-pi-agent-dir.d.ts.map +1 -0
- package/dist/managed-pi-agent-dir.js +66 -0
- package/dist/managed-pi-agent-dir.js.map +1 -0
- package/dist/managed-pi-bootstrap.d.ts +24 -0
- package/dist/managed-pi-bootstrap.d.ts.map +1 -0
- package/dist/managed-pi-bootstrap.js +135 -0
- package/dist/managed-pi-bootstrap.js.map +1 -0
- package/dist/missing-tool-recovery-runner.d.ts +23 -0
- package/dist/missing-tool-recovery-runner.d.ts.map +1 -0
- package/dist/missing-tool-recovery-runner.js +73 -0
- package/dist/missing-tool-recovery-runner.js.map +1 -0
- package/dist/pi-adapter.d.ts +134 -0
- package/dist/pi-adapter.d.ts.map +1 -0
- package/dist/pi-adapter.js +1648 -0
- package/dist/pi-adapter.js.map +1 -0
- package/dist/pi-branch-guard.d.ts +39 -0
- package/dist/pi-branch-guard.d.ts.map +1 -0
- package/dist/pi-branch-guard.js +75 -0
- package/dist/pi-branch-guard.js.map +1 -0
- package/dist/pi-event-reporter.d.ts +15 -0
- package/dist/pi-event-reporter.d.ts.map +1 -0
- package/dist/pi-event-reporter.js +136 -0
- package/dist/pi-event-reporter.js.map +1 -0
- package/dist/pi-resource-bundle.d.ts +12 -0
- package/dist/pi-resource-bundle.d.ts.map +1 -0
- package/dist/pi-resource-bundle.js +58 -0
- package/dist/pi-resource-bundle.js.map +1 -0
- package/dist/pre-turn-targeted-inputs.d.ts +44 -0
- package/dist/pre-turn-targeted-inputs.d.ts.map +1 -0
- package/dist/pre-turn-targeted-inputs.js +101 -0
- package/dist/pre-turn-targeted-inputs.js.map +1 -0
- package/dist/prompt-guards.d.ts +52 -0
- package/dist/prompt-guards.d.ts.map +1 -0
- package/dist/prompt-guards.js +318 -0
- package/dist/prompt-guards.js.map +1 -0
- package/dist/question-bridge.d.ts +16 -0
- package/dist/question-bridge.d.ts.map +1 -0
- package/dist/question-bridge.js +55 -0
- package/dist/question-bridge.js.map +1 -0
- package/dist/question-tool.d.ts +14 -0
- package/dist/question-tool.d.ts.map +1 -0
- package/dist/question-tool.js +54 -0
- package/dist/question-tool.js.map +1 -0
- package/dist/resource-loader-factory.d.ts +11 -0
- package/dist/resource-loader-factory.d.ts.map +1 -0
- package/dist/resource-loader-factory.js +87 -0
- package/dist/resource-loader-factory.js.map +1 -0
- package/dist/result-image-upload.d.ts +14 -0
- package/dist/result-image-upload.d.ts.map +1 -0
- package/dist/result-image-upload.js +183 -0
- package/dist/result-image-upload.js.map +1 -0
- package/dist/runtime/adapters.d.ts +48 -0
- package/dist/runtime/adapters.d.ts.map +1 -0
- package/dist/runtime/adapters.js +15 -0
- package/dist/runtime/adapters.js.map +1 -0
- package/dist/runtime/bootstrap-session.d.ts +95 -0
- package/dist/runtime/bootstrap-session.d.ts.map +1 -0
- package/dist/runtime/bootstrap-session.js +198 -0
- package/dist/runtime/bootstrap-session.js.map +1 -0
- package/dist/runtime/bootstrap.d.ts +81 -0
- package/dist/runtime/bootstrap.d.ts.map +1 -0
- package/dist/runtime/bootstrap.js +440 -0
- package/dist/runtime/bootstrap.js.map +1 -0
- package/dist/runtime/failure-policy.d.ts +79 -0
- package/dist/runtime/failure-policy.d.ts.map +1 -0
- package/dist/runtime/failure-policy.js +182 -0
- package/dist/runtime/failure-policy.js.map +1 -0
- package/dist/runtime/index.d.ts +9 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +326 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/lifecycle-reducer.d.ts +328 -0
- package/dist/runtime/lifecycle-reducer.d.ts.map +1 -0
- package/dist/runtime/lifecycle-reducer.js +1100 -0
- package/dist/runtime/lifecycle-reducer.js.map +1 -0
- package/dist/runtime/llm-turn-execution.d.ts +42 -0
- package/dist/runtime/llm-turn-execution.d.ts.map +1 -0
- package/dist/runtime/llm-turn-execution.js +472 -0
- package/dist/runtime/llm-turn-execution.js.map +1 -0
- package/dist/runtime/settings.d.ts +11 -0
- package/dist/runtime/settings.d.ts.map +1 -0
- package/dist/runtime/settings.js +23 -0
- package/dist/runtime/settings.js.map +1 -0
- package/dist/runtime/snapshot-policy.d.ts +11 -0
- package/dist/runtime/snapshot-policy.d.ts.map +1 -0
- package/dist/runtime/snapshot-policy.js +16 -0
- package/dist/runtime/snapshot-policy.js.map +1 -0
- package/dist/runtime/turn-execution.d.ts +49 -0
- package/dist/runtime/turn-execution.d.ts.map +1 -0
- package/dist/runtime/turn-execution.js +120 -0
- package/dist/runtime/turn-execution.js.map +1 -0
- package/dist/session-snapshot-exchange.d.ts +39 -0
- package/dist/session-snapshot-exchange.d.ts.map +1 -0
- package/dist/session-snapshot-exchange.js +102 -0
- package/dist/session-snapshot-exchange.js.map +1 -0
- package/dist/test-helpers/fake-websocket.d.ts +16 -0
- package/dist/test-helpers/fake-websocket.d.ts.map +1 -0
- package/dist/test-helpers/fake-websocket.js +40 -0
- package/dist/test-helpers/fake-websocket.js.map +1 -0
- package/dist/test-helpers/ipc-harness.d.ts +11 -0
- package/dist/test-helpers/ipc-harness.d.ts.map +1 -0
- package/dist/test-helpers/ipc-harness.js +17 -0
- package/dist/test-helpers/ipc-harness.js.map +1 -0
- package/dist/test-helpers/worker-message-builders.d.ts +21 -0
- package/dist/test-helpers/worker-message-builders.d.ts.map +1 -0
- package/dist/test-helpers/worker-message-builders.js +62 -0
- package/dist/test-helpers/worker-message-builders.js.map +1 -0
- package/dist/turn-continuation.d.ts +28 -0
- package/dist/turn-continuation.d.ts.map +1 -0
- package/dist/turn-continuation.js +80 -0
- package/dist/turn-continuation.js.map +1 -0
- package/dist/turn-execution-error.d.ts +39 -0
- package/dist/turn-execution-error.d.ts.map +1 -0
- package/dist/turn-execution-error.js +64 -0
- package/dist/turn-execution-error.js.map +1 -0
- package/dist/turn-execution-result.d.ts +35 -0
- package/dist/turn-execution-result.d.ts.map +1 -0
- package/dist/turn-execution-result.js +21 -0
- package/dist/turn-execution-result.js.map +1 -0
- package/dist/turn-outcome-tool-session.d.ts +50 -0
- package/dist/turn-outcome-tool-session.d.ts.map +1 -0
- package/dist/turn-outcome-tool-session.js +209 -0
- package/dist/turn-outcome-tool-session.js.map +1 -0
- package/dist/turn-record-id.d.ts +3 -0
- package/dist/turn-record-id.d.ts.map +1 -0
- package/dist/turn-record-id.js +10 -0
- package/dist/turn-record-id.js.map +1 -0
- package/dist/turn-result-markdown.d.ts +74 -0
- package/dist/turn-result-markdown.d.ts.map +1 -0
- package/dist/turn-result-markdown.js +263 -0
- package/dist/turn-result-markdown.js.map +1 -0
- package/dist/turn-tree-strategy.d.ts +29 -0
- package/dist/turn-tree-strategy.d.ts.map +1 -0
- package/dist/turn-tree-strategy.js +80 -0
- package/dist/turn-tree-strategy.js.map +1 -0
- package/dist/worker-entry.d.ts +2 -0
- package/dist/worker-entry.d.ts.map +1 -0
- package/dist/worker-entry.js +7 -0
- package/dist/worker-entry.js.map +1 -0
- package/dist/worker-http.d.ts +2 -0
- package/dist/worker-http.d.ts.map +1 -0
- package/dist/worker-http.js +9 -0
- package/dist/worker-http.js.map +1 -0
- package/dist/worker-ipc-reporter.d.ts +29 -0
- package/dist/worker-ipc-reporter.d.ts.map +1 -0
- package/dist/worker-ipc-reporter.js +58 -0
- package/dist/worker-ipc-reporter.js.map +1 -0
- package/dist/worker-payloads.d.ts +10 -0
- package/dist/worker-payloads.d.ts.map +1 -0
- package/dist/worker-payloads.js +66 -0
- package/dist/worker-payloads.js.map +1 -0
- package/dist/worker-start-payload.d.ts +7 -0
- package/dist/worker-start-payload.d.ts.map +1 -0
- package/dist/worker-start-payload.js +5 -0
- package/dist/worker-start-payload.js.map +1 -0
- package/dist/workspace/manifest.d.ts +35 -0
- package/dist/workspace/manifest.d.ts.map +1 -0
- package/dist/workspace/manifest.js +79 -0
- package/dist/workspace/manifest.js.map +1 -0
- package/dist/workspace/node-run-root-git-ops.d.ts +17 -0
- package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -0
- package/dist/workspace/node-run-root-git-ops.js +96 -0
- package/dist/workspace/node-run-root-git-ops.js.map +1 -0
- package/dist/workspace/resource-aggregator.d.ts +15 -0
- package/dist/workspace/resource-aggregator.d.ts.map +1 -0
- package/dist/workspace/resource-aggregator.js +39 -0
- package/dist/workspace/resource-aggregator.js.map +1 -0
- package/dist/workspace/run-root.d.ts +52 -0
- package/dist/workspace/run-root.d.ts.map +1 -0
- package/dist/workspace/run-root.js +209 -0
- package/dist/workspace/run-root.js.map +1 -0
- package/package.json +55 -0
- package/src/automatic-turn-executor.ts +106 -0
- package/src/diagnostics.ts +82 -0
- package/src/entry-runtime.ts +235 -0
- package/src/event-translator.ts +22 -0
- package/src/extension-entry-roots.ts +43 -0
- package/src/identified-pi-entry.ts +188 -0
- package/src/index.ts +54 -0
- package/src/input-consumer.ts +71 -0
- package/src/ipc.ts +250 -0
- package/src/logical-prompt-runner.ts +320 -0
- package/src/managed-pi-agent-dir.ts +86 -0
- package/src/managed-pi-bootstrap.ts +208 -0
- package/src/missing-tool-recovery-runner.ts +112 -0
- package/src/pi-adapter.ts +2197 -0
- package/src/pi-branch-guard.ts +104 -0
- package/src/pi-event-reporter.ts +164 -0
- package/src/pi-resource-bundle.ts +90 -0
- package/src/pre-turn-targeted-inputs.ts +167 -0
- package/src/prompt-guards.ts +385 -0
- package/src/question-bridge.ts +69 -0
- package/src/question-tool.ts +65 -0
- package/src/resource-loader-factory.ts +115 -0
- package/src/result-image-upload.ts +256 -0
- package/src/runtime/adapters.ts +67 -0
- package/src/runtime/bootstrap-session.ts +294 -0
- package/src/runtime/bootstrap.ts +621 -0
- package/src/runtime/failure-policy.ts +274 -0
- package/src/runtime/index.ts +362 -0
- package/src/runtime/lifecycle-reducer.ts +1537 -0
- package/src/runtime/llm-turn-execution.ts +696 -0
- package/src/runtime/settings.ts +41 -0
- package/src/runtime/snapshot-policy.ts +28 -0
- package/src/runtime/turn-execution.ts +203 -0
- package/src/session-snapshot-exchange.ts +155 -0
- package/src/test-helpers/fake-websocket.ts +46 -0
- package/src/test-helpers/ipc-harness.ts +19 -0
- package/src/test-helpers/worker-message-builders.ts +92 -0
- package/src/turn-continuation.ts +147 -0
- package/src/turn-execution-error.ts +102 -0
- package/src/turn-execution-result.ts +48 -0
- package/src/turn-outcome-tool-session.ts +311 -0
- package/src/turn-record-id.ts +17 -0
- package/src/turn-result-markdown.ts +355 -0
- package/src/turn-tree-strategy.ts +141 -0
- package/src/worker-entry.ts +8 -0
- package/src/worker-http.ts +6 -0
- package/src/worker-ipc-reporter.ts +100 -0
- package/src/worker-payloads.ts +94 -0
- package/src/worker-start-payload.ts +10 -0
- package/src/workspace/manifest.ts +126 -0
- package/src/workspace/node-run-root-git-ops.ts +137 -0
- package/src/workspace/resource-aggregator.ts +43 -0
- package/src/workspace/run-root.ts +361 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
type ComponentManifest,
|
|
4
|
+
type ComponentManifestEntry,
|
|
5
|
+
deserializeManifest,
|
|
6
|
+
diffManifest,
|
|
7
|
+
type ManifestDiff,
|
|
8
|
+
serializeManifest,
|
|
9
|
+
} from "./manifest.js";
|
|
10
|
+
import { aggregateAgentsMd, collectSkills } from "./resource-aggregator.js";
|
|
11
|
+
|
|
12
|
+
export interface GitOps {
|
|
13
|
+
clone(repoLocator: string, targetDir: string): Promise<void>;
|
|
14
|
+
checkout(repoDir: string, branch: string): Promise<void>;
|
|
15
|
+
createBranch(repoDir: string, branchName: string, startPoint: string): Promise<void>;
|
|
16
|
+
branchExists(repoDir: string, branchName: string): Promise<boolean>;
|
|
17
|
+
getHeadSha(repoDir: string): Promise<string>;
|
|
18
|
+
readFile(repoDir: string, filePath: string): Promise<string | null>;
|
|
19
|
+
listFiles(repoDir: string, pattern: string): Promise<string[]>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type RunRootGitOps = GitOps & {
|
|
23
|
+
writeFile(repoDir: string, filePath: string, content: string): Promise<void>;
|
|
24
|
+
configureRepositoryCredentials?(
|
|
25
|
+
credentials: readonly import("@leitwerk-dev/worker-protocol").WorkerGitSshCredential[],
|
|
26
|
+
): void;
|
|
27
|
+
cleanupRepositoryCredentials?(): void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export interface RunRootPlan {
|
|
31
|
+
workspaceRoot: string;
|
|
32
|
+
instanceId: string;
|
|
33
|
+
components: Array<{
|
|
34
|
+
key: string;
|
|
35
|
+
repoLocator: string;
|
|
36
|
+
baseBranch: string;
|
|
37
|
+
workBranch: string;
|
|
38
|
+
}>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface MaterializeResult {
|
|
42
|
+
ok: boolean;
|
|
43
|
+
manifest: ComponentManifest;
|
|
44
|
+
aggregatedAgentsMdSources: string[];
|
|
45
|
+
loadedSkills: string[];
|
|
46
|
+
errors: string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ValidationResult {
|
|
50
|
+
valid: boolean;
|
|
51
|
+
diff: ManifestDiff;
|
|
52
|
+
existingManifest: ComponentManifest | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const MANIFEST_REL = path.join(".leitwerk", "components.json");
|
|
56
|
+
const AGGREGATED_AGENTS_REL = "AGENTS.md";
|
|
57
|
+
const SKILLS_DIR_REL = path.join(".leitwerk", "skills");
|
|
58
|
+
|
|
59
|
+
const SKILL_GLOB = "**/.cursor/skills/**/SKILL.md";
|
|
60
|
+
const AGENTS_GLOB = "**/AGENTS.md";
|
|
61
|
+
|
|
62
|
+
function isPathInside(parent: string, child: string): boolean {
|
|
63
|
+
const relative = path.relative(parent, child);
|
|
64
|
+
return (
|
|
65
|
+
relative !== "" &&
|
|
66
|
+
relative !== ".." &&
|
|
67
|
+
!relative.startsWith(`..${path.sep}`) &&
|
|
68
|
+
!path.isAbsolute(relative)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function safeJoinInsideWorkspace(workspaceRoot: string, childPath: string): string {
|
|
73
|
+
const root = path.resolve(workspaceRoot);
|
|
74
|
+
const target = path.resolve(root, childPath);
|
|
75
|
+
if (!isPathInside(root, target)) {
|
|
76
|
+
throw new Error(`Component key '${childPath}' resolves outside workspace root '${root}'`);
|
|
77
|
+
}
|
|
78
|
+
return target;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function componentDir(workspaceRoot: string, key: string): string {
|
|
82
|
+
return safeJoinInsideWorkspace(workspaceRoot, key);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function skillIdFromSkillPath(filePath: string): string | null {
|
|
86
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
87
|
+
const m = normalized.match(/\.cursor\/skills\/([^/]+)\/SKILL\.md$/);
|
|
88
|
+
return m?.[1] ?? null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function planRunRoot(
|
|
92
|
+
workspaceRoot: string,
|
|
93
|
+
instanceId: string,
|
|
94
|
+
projectSnapshots: Array<{
|
|
95
|
+
key: string;
|
|
96
|
+
repoLocator: string;
|
|
97
|
+
baseBranch: string;
|
|
98
|
+
workBranch: string;
|
|
99
|
+
}>,
|
|
100
|
+
): RunRootPlan {
|
|
101
|
+
return {
|
|
102
|
+
workspaceRoot,
|
|
103
|
+
instanceId,
|
|
104
|
+
components: projectSnapshots.map((p) => ({ ...p })),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function materializeComponentEntry(
|
|
109
|
+
plan: RunRootPlan,
|
|
110
|
+
comp: RunRootPlan["components"][number],
|
|
111
|
+
git: RunRootGitOps,
|
|
112
|
+
): Promise<ComponentManifestEntry> {
|
|
113
|
+
const dir = componentDir(plan.workspaceRoot, comp.key);
|
|
114
|
+
await git.clone(comp.repoLocator, dir);
|
|
115
|
+
await git.checkout(dir, comp.baseBranch);
|
|
116
|
+
if (await git.branchExists(dir, comp.workBranch)) {
|
|
117
|
+
await git.checkout(dir, comp.workBranch);
|
|
118
|
+
} else {
|
|
119
|
+
await git.createBranch(dir, comp.workBranch, comp.baseBranch);
|
|
120
|
+
await git.checkout(dir, comp.workBranch);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
key: comp.key,
|
|
124
|
+
repoLocator: comp.repoLocator,
|
|
125
|
+
baseBranch: comp.baseBranch,
|
|
126
|
+
workBranch: comp.workBranch,
|
|
127
|
+
clonedAt: new Date().toISOString(),
|
|
128
|
+
headSha: await git.getHeadSha(dir),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function collectMaterializedEntries(
|
|
133
|
+
plan: RunRootPlan,
|
|
134
|
+
git: RunRootGitOps,
|
|
135
|
+
errors: string[],
|
|
136
|
+
shouldMaterialize: (componentKey: string) => boolean = () => true,
|
|
137
|
+
existingEntryForKey?: (componentKey: string) => ComponentManifestEntry | null,
|
|
138
|
+
): Promise<ComponentManifestEntry[]> {
|
|
139
|
+
const entries: ComponentManifestEntry[] = [];
|
|
140
|
+
for (const comp of plan.components) {
|
|
141
|
+
if (!shouldMaterialize(comp.key)) {
|
|
142
|
+
const existing = existingEntryForKey?.(comp.key) ?? null;
|
|
143
|
+
if (existing) {
|
|
144
|
+
entries.push(existing);
|
|
145
|
+
} else {
|
|
146
|
+
errors.push(`${comp.key}: no existing manifest entry for unchanged key`);
|
|
147
|
+
}
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
entries.push(await materializeComponentEntry(plan, comp, git));
|
|
152
|
+
} catch (e) {
|
|
153
|
+
errors.push(`${comp.key}: ${e instanceof Error ? e.message : String(e)}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return entries;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function gatherAgentsAndSkills(
|
|
160
|
+
plan: RunRootPlan,
|
|
161
|
+
git: RunRootGitOps,
|
|
162
|
+
): Promise<{
|
|
163
|
+
aggregatedAgentsMdSources: string[];
|
|
164
|
+
agentsMd: string;
|
|
165
|
+
skillsMap: Map<string, { content: string; source: string }>;
|
|
166
|
+
}> {
|
|
167
|
+
const agentSources: Array<{
|
|
168
|
+
componentKey: string;
|
|
169
|
+
filePath: string;
|
|
170
|
+
content: string;
|
|
171
|
+
}> = [];
|
|
172
|
+
const skillSources: Array<{
|
|
173
|
+
componentKey: string;
|
|
174
|
+
skillId: string;
|
|
175
|
+
content: string;
|
|
176
|
+
}> = [];
|
|
177
|
+
const aggregatedAgentsMdSources: string[] = [];
|
|
178
|
+
|
|
179
|
+
for (const comp of plan.components) {
|
|
180
|
+
const root = componentDir(plan.workspaceRoot, comp.key);
|
|
181
|
+
const agentPaths = await git.listFiles(root, AGENTS_GLOB);
|
|
182
|
+
const sortedAgentPaths = [...agentPaths].sort((a, b) => a.localeCompare(b));
|
|
183
|
+
for (const p of sortedAgentPaths) {
|
|
184
|
+
const content = await git.readFile(root, p);
|
|
185
|
+
if (content !== null) {
|
|
186
|
+
agentSources.push({
|
|
187
|
+
componentKey: comp.key,
|
|
188
|
+
filePath: p,
|
|
189
|
+
content,
|
|
190
|
+
});
|
|
191
|
+
aggregatedAgentsMdSources.push(`${comp.key}/${p}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const skillPaths = await git.listFiles(root, SKILL_GLOB);
|
|
196
|
+
for (const p of skillPaths) {
|
|
197
|
+
const skillId = skillIdFromSkillPath(p);
|
|
198
|
+
if (!skillId) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const content = await git.readFile(root, p);
|
|
202
|
+
if (content !== null) {
|
|
203
|
+
skillSources.push({ componentKey: comp.key, skillId, content });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const agentsMd = aggregateAgentsMd(agentSources);
|
|
209
|
+
const skillsMap = collectSkills(skillSources);
|
|
210
|
+
aggregatedAgentsMdSources.sort((a, b) => a.localeCompare(b));
|
|
211
|
+
return { aggregatedAgentsMdSources, agentsMd, skillsMap };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function writeAggregatedArtifacts(
|
|
215
|
+
plan: RunRootPlan,
|
|
216
|
+
git: RunRootGitOps,
|
|
217
|
+
manifest: ComponentManifest,
|
|
218
|
+
agentsMd: string,
|
|
219
|
+
skillsMap: Map<string, { content: string; source: string }>,
|
|
220
|
+
): Promise<void> {
|
|
221
|
+
const root = plan.workspaceRoot;
|
|
222
|
+
await git.writeFile(root, MANIFEST_REL, serializeManifest(manifest));
|
|
223
|
+
await git.writeFile(root, AGGREGATED_AGENTS_REL, agentsMd);
|
|
224
|
+
for (const [skillId, { content }] of skillsMap) {
|
|
225
|
+
const rel = path.join(SKILLS_DIR_REL, `${skillId}.md`);
|
|
226
|
+
await git.writeFile(root, rel, content);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function finalizeRunRoot(
|
|
231
|
+
plan: RunRootPlan,
|
|
232
|
+
git: RunRootGitOps,
|
|
233
|
+
entries: ComponentManifestEntry[],
|
|
234
|
+
errors: string[],
|
|
235
|
+
) {
|
|
236
|
+
const manifest: ComponentManifest = {
|
|
237
|
+
version: 1,
|
|
238
|
+
instanceId: plan.instanceId,
|
|
239
|
+
createdAt: new Date().toISOString(),
|
|
240
|
+
components: entries,
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const { aggregatedAgentsMdSources, agentsMd, skillsMap } = await gatherAgentsAndSkills(plan, git);
|
|
244
|
+
const loadedSkills = [...skillsMap.keys()].sort((a, b) => a.localeCompare(b));
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
await writeAggregatedArtifacts(plan, git, manifest, agentsMd, skillsMap);
|
|
248
|
+
} catch (e) {
|
|
249
|
+
errors.push(`write: ${e instanceof Error ? e.message : String(e)}`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return { manifest, aggregatedAgentsMdSources, loadedSkills };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export async function materializeRunRoot(
|
|
256
|
+
plan: RunRootPlan,
|
|
257
|
+
git: RunRootGitOps,
|
|
258
|
+
): Promise<MaterializeResult> {
|
|
259
|
+
const errors: string[] = [];
|
|
260
|
+
const entries = await collectMaterializedEntries(plan, git, errors);
|
|
261
|
+
const { manifest, aggregatedAgentsMdSources, loadedSkills } = await finalizeRunRoot(
|
|
262
|
+
plan,
|
|
263
|
+
git,
|
|
264
|
+
entries,
|
|
265
|
+
errors,
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
const ok = errors.length === 0 && plan.components.length === entries.length;
|
|
269
|
+
return {
|
|
270
|
+
ok,
|
|
271
|
+
manifest,
|
|
272
|
+
aggregatedAgentsMdSources,
|
|
273
|
+
loadedSkills,
|
|
274
|
+
errors,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export async function validateRunRoot(
|
|
279
|
+
workspaceRoot: string,
|
|
280
|
+
serverProjects: Array<{
|
|
281
|
+
key: string;
|
|
282
|
+
repoLocator: string;
|
|
283
|
+
baseBranch: string;
|
|
284
|
+
workBranch: string;
|
|
285
|
+
}>,
|
|
286
|
+
git: GitOps,
|
|
287
|
+
): Promise<ValidationResult> {
|
|
288
|
+
const raw = await git.readFile(workspaceRoot, MANIFEST_REL);
|
|
289
|
+
if (raw === null) {
|
|
290
|
+
return {
|
|
291
|
+
valid: false,
|
|
292
|
+
diff: {
|
|
293
|
+
stale: [],
|
|
294
|
+
missing: serverProjects.map((p) => p.key),
|
|
295
|
+
extra: [],
|
|
296
|
+
unchanged: [],
|
|
297
|
+
},
|
|
298
|
+
existingManifest: null,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
const parsed = deserializeManifest(raw);
|
|
302
|
+
if (!parsed.ok) {
|
|
303
|
+
return {
|
|
304
|
+
valid: false,
|
|
305
|
+
diff: {
|
|
306
|
+
stale: [],
|
|
307
|
+
missing: serverProjects.map((p) => p.key),
|
|
308
|
+
extra: [],
|
|
309
|
+
unchanged: [],
|
|
310
|
+
},
|
|
311
|
+
existingManifest: null,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
const diff = diffManifest(parsed.manifest, serverProjects);
|
|
315
|
+
const valid = diff.stale.length === 0 && diff.missing.length === 0 && diff.extra.length === 0;
|
|
316
|
+
return {
|
|
317
|
+
valid,
|
|
318
|
+
diff,
|
|
319
|
+
existingManifest: parsed.manifest,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export async function repairRunRoot(
|
|
324
|
+
_workspaceRoot: string,
|
|
325
|
+
validation: ValidationResult,
|
|
326
|
+
plan: RunRootPlan,
|
|
327
|
+
git: RunRootGitOps,
|
|
328
|
+
): Promise<MaterializeResult> {
|
|
329
|
+
const errors: string[] = [];
|
|
330
|
+
const toRepair = new Set([...validation.diff.stale, ...validation.diff.missing]);
|
|
331
|
+
const manifestEntries = await collectMaterializedEntries(
|
|
332
|
+
plan,
|
|
333
|
+
git,
|
|
334
|
+
errors,
|
|
335
|
+
(componentKey) => toRepair.has(componentKey),
|
|
336
|
+
(componentKey) =>
|
|
337
|
+
validation.existingManifest?.components.find((component) => component.key === componentKey) ??
|
|
338
|
+
null,
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
const { manifest, aggregatedAgentsMdSources, loadedSkills } = await finalizeRunRoot(
|
|
342
|
+
plan,
|
|
343
|
+
git,
|
|
344
|
+
manifestEntries,
|
|
345
|
+
errors,
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
const expectedKeys = new Set(plan.components.map((c) => c.key));
|
|
349
|
+
const ok =
|
|
350
|
+
errors.length === 0 &&
|
|
351
|
+
manifestEntries.length === plan.components.length &&
|
|
352
|
+
[...expectedKeys].every((k) => manifestEntries.some((e) => e.key === k));
|
|
353
|
+
|
|
354
|
+
return {
|
|
355
|
+
ok,
|
|
356
|
+
manifest,
|
|
357
|
+
aggregatedAgentsMdSources,
|
|
358
|
+
loadedSkills,
|
|
359
|
+
errors,
|
|
360
|
+
};
|
|
361
|
+
}
|